Concatenate two columns based upon another column in SQL Server 2005 - sql-server-2005

I have database as follows
A B
1 ereee
2 ereee
2 sdfsd
3 nere
1 sdfsd
I want to have a data which will be as follows
A B
1 ereee,sdfsd
2 ereee,sdfsd
3 nere
what is the query?

Your question didn't specify what kind of database. The following works for MySQL and perhaps a few others:
GROUP_CONCAT(expr)
This function returns a string result with the concatenated non-NULL values from a group.
https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
SELECT A, GROUP_CONCAT(B SEPARATOR ',') AS B
FROM table
GROUP BY A
Update 2015-07-30 09:43 +0000
Since it's been made clear that this is about SQL Server 2005, the question might be a duplicate of this:
Simulating group_concat MySQL function in Microsoft SQL Server 2005?

Related

how to implement multiple if-and function into a sql query?

These two tables below are in the same excel file, but in my database both of them are separated into two different tables.
The first table looks like this:
table1
a_actual b_actual c_actual no
2020-04-21 null 2020-05-23 1
null 2020-04-22 2020-04-27 2
While the second table looks like this:
table2
a 30
b 40
c 100
Both tables are combined and operated with if-and function in excel to get each weight from table 2.
the if function in excel is:
=if(and(a_actual>0; a_actual<=now()); a; (if(and(b_actual>0; b_actual<= now()); b; (if(and(c_actual>0; c_actual<=now());c;0))))
Assuming that the current date is today: 2021-06-08, then the expected result:
no weight
1 100
2 100
I need to implement this if-and function of excel into a sql query.
I supposed I can use case when, but I don't know exactly how to write the query properly.
How can I implement the if-and function into a sql query?
Thank you very much for any help.

Pull specific numbers from a column that has numbers and words

Column
1
7
f
3
2
c
1
d
6
4
e
g
b
I want to be able to filter this using the IN() operator in the where clause and pull out only the numbers. The column is a varchar so it is coming back as an error in postgres
select substring(colname FROM '[0-9]+') from tablename
You can filter the numbers using the ISNUMERIC() function on the WHERE Clausule.
Something like this:
SELECT *
FROM Table1
WHERE ISNUMERIC(column_name)=1
As mentioned on the comments, this is for SQL Server, but you can create your own ISNUMERIC function in PostgreSQL following this example:
isnumeric() with PostgreSQL
I ended up subquerying with this in the SELECT --- cast(substring(column FROM '[0-9]+') as int) and this in the WHERE column ~ '^\d+$' in the FROM as its own table. Pulling just the integers i needed from that with IN (1,2,3)

How to show blank instead of column value for all duplicated columns of a SQL query?

There is a similar question which answer this for a known number of columns and only a single selection column. But the problem here is that
I have no knowledge of columns (count, type) of a specified SQL query and also I want to blank for all columns not a single column.
For example lets say I have following query.
Select * from View1
Result :
Column(1) Column(2) Column(..) Column(N)
1 A Sales 1500
2 C Sales 2500
3 C Sales 2500
4 A Development 2500
Expected result :
Column(1) Column(2) Column(..) Column(N)
1 A Sales 1500
2 C 2500
3
4 A Development
Pseudo SQL Query :
EXEC proc_blank_query_result 'Select * from View1'
If you're in SQL Server 2012 or newer, you can do this with lag, something like this:
select
nullif(column1, lag(column1) over (order by yourorderbyclause)) as column1,
nullif(column2, lag(column2) over (order by yourorderbyclause)) as column2,
...
from
View1
To make it dynamic, well then you have to parse a lot of metadata from the query. Using sp_describe_first_result_set might be a good idea, or use select into a temp. table and parse the columns of it.

To select a unique value from a column having multiple values MS Access

Table Structure is like this
Table Name:Employee
**Employee_Id APPRS_TY_CD**
540589 2
540589 UNK
1952938 2
1952938 UNK
2488178 1
2488178 UNK
3818934 1
3818934 UNK
5402944 1
If a Employee ID has APPRS_TY_CD as (UNK AND a value) then APPRS_TY_CD should be a value and not UNK. If APPRS_TY_CD is not UNK for an Employee ID then that value should be populated as it is.
My final output should look like this.
**Employee_Id APPRS_TY_CD**
540589 2
1952938 2
2488178 1
3818934 1
5402944 1
I'm using MS Access.
This should be fairly simple as numbers are considered "lower" than strings you can use an aggregate function, I've created an SQL Fiddle here (note this is Sql Server but the code should be the same as it's not using proprietary features). Given your data you could use the MIN function from SQL to get the APPRS_TY_CD for each user. Here is my suggested code:
SELECT
Employee_Id
, MIN(APPRS_TY_CD) APPRS_TY_CD
FROM
Employee
GROUP BY
Employee_Id
The results returned are (you should be able toe execute the fiddle yourself to prove this):
EMPLOYEE_ID APPRS_TY_CD
540589 2
1952938 2
2488178 1
3818934 1
5402944 1

SQL Select where id is in `column`

I have a column that has multiple numbers separated by a comma. Example for a row:
`numbers`:
1,2,6,66,4,9
I want to make a query that will select the row only if the number 6 (for example) is in the column numbers.
I cant use LIKE because if there is 66 it'll work too.
You can use like. Concatenate the field separators at the beginning and end of the list and then use like. Here is the SQL Server sytnax:
where ','+numbers+',' like '%,'+'6'+',%'
SQL Server uses + for string concatenation. Other databases use || or the concat() function.
You should change your database to rather have a new table that joins numbers with the row of your current table. So if your row looks like this:
id numbers
1 1,2,6,66,4,9
You would have a new table that joins those values like so
row_id number
1 1
1 2
1 6
1 66
1 4
1 9
Then you can search for the number 6 in the number column and get the row_id