SQL union grouped by rows - sql

Assume I have a table like this:
col1
col2
col3
col4
commonrow
one
two
null
commonrow
null
null
three
How to produce a result to look like this:
col1
col2
col3
col4
commonrow
one
two
three
Thanks

like this, you can group by col1 and get the maximum in each group:
select col1 , max(col2) col2 , max(col3) col3 , max(col4) col4
from table
group by col1

Related

Match delimited values from two columns

col1 col2 col3
21 12;13;45;67; 65;43;66;34;33
23 45;67;23;13; 45;78;89;09;32
I have something like above which I got from so many joins. One of my where condition is to check col1 is in col2 which I did with:
... col1 IN (SELECT value FROM STRING_SPLIT(col2, ';')`.
I have or condition to check col2 is in col3. How can I do that in sql.
Goal is col2 should be either in col1 or col3.
A query like this works:
SELECT value
FROM yourdata cross apply string_split(col2, ';')
where value in (col1)
and value in (SELECT value
FROM yourdata cross apply string_split(col3, ';'))
Data:
Col1 Col2 Col3
-----------------------------------------
21 12;13;45;67; 65;43;66;34;23 -- notice 23 in col3
23 45;67;23;13; 45;78;89;09;32

How to create a select query with multiple columns querying a single column with multiple conditions

I want to get a list of results from my db2 database querying a table having multiple conditions on a same column. Here is an example scenario:
Table1:
col1, col2, col3, col4, col5
sample, user, adam, ggg, hdh
sample, source, online, urt, loe
random, user, henr, uuu, kkk
crew, user, mike, nhg, kik
crew, dummy, nothing, irr, wer
ment, maker, hts, ret, opp
I am trying to write a select query that will list me the details in the following manner:
col1, col2, col3, col4
sample, adam, online, ggg
random, henr, , uuu
crew, mike, nothing, irr
ment, , , ret
So when col2 has the value user I want the corresponding col3 value as one of the column to my select statement. And, when col2 has value in source or dummy, I want the corresponding col3 value as another column to my select statement. In this way, I can finally have a list with 4 columns which gets me the unique combination of details that I need.
I have a draft sql query of this kind. I am not sure how to refactor this to get two output columns querying a single column.
select col1 , col3 as col2, col3 as col3, col4 from Table1
where col2='user' or col2 in ('source','dummy')
If I do this, I am getting the output in this manner
col1, col2, col3, col4
sample, adam, adam, ggg
sample, online, online, ggg
random, henr, henr, uuu
crew, mike, mike, irr
crew, nothing, nothing, irr
I think you want this:
select col1,
max(case when col2 = 'user' then col3 end) as col2,
max(case when col2 in ('source', 'dummy') then col3 end) as col3,
max(col4) as col4
from t
group by col1;
I'm not sure why you've mentioned col5 in your input as it's not referred to in your output? It just muddies the water ;-)
Anyway, here's the SQL you need to get the results you're after based on the input/output example:
WITH
USER AS
( SELECT COL1
, COL3 AS COLA
, COL4 AS COLB
FROM TABLE1
WHERE COL2 = 'USER'
)
, SOURCE AS
( SELECT COL1
, COL2 AS COLC
, COL3 AS COLD
, COL4 AS COLE
FROM TABLE1
WHERE COL2 IN ( 'SOURCE'
, 'DUMMY'
)
)
SELECT U.COL1
, U.COLA AS COL2
, COALESCE(S.COLD,'') AS COL3
, COALESCE
( CASE
WHEN S.COLC = 'DUMMY'
THEN S.COLE
ELSE U.COLB
END
, U.COLB
) AS COL4
FROM USER U
LEFT OUTER JOIN SOURCE S
ON S.COL1 = U.COL1
WITH UR FOR FETCH ONLY;
may be you need to write select col1 , col3 as col2, col2 as col3, col4 from Table1... and then add respective conditions , as in your case. second and third columns in output are same because you are selecting col3 in both

SQL Server: SELECT IF conditionally

I have a table with different columns and I need to show different based on the value of a specific column.
My table It's like this:
select col1, col2, col3, col4, col5, col6
from table1
So, for example:
if col1 = '1' --> show col3 and col4.
if col2 = '1' --> show col5 and col6
Use case expressions to chose columns to return. Note that a case's return types must be compatible, i.e. col3 and col5, and col4 and col6.
select case when col1 = 1 then col3
when col2 = 1 then col5
end,
case when col1 = 1 then col4
when col2 = 1 then col6
end
from tablename
Or, do a UNION ALL:
select col3, col4
from tablename where col1 = 1
union all
select col5, col6
from tablename where col2 = 1
Remaining questions - what do to if both col1 and col2 are 1? Or if none of them are?
Rule of thumb 1 : return to the caller the columns used for filtering.
Rule of thumb 2 : don't use union all, it returns duplicates and your result will not be a 'relation' (though in this case there would be no duplicates because col1 is a key but I still think union states the intent best).
select col1, col2, col3 AS colum_a, col4 AS colum_b
from table1
where col1 = 1
union
select col1, col2, col5 AS colum_a, col6 AS colum_b
from table1
where col2 = 1;

Greatest of three columns in SQL

I want to compare multiple columns and come with the maximum value among different columns
For three columns I came up with the below query which works fine
SELECT CASE
WHEN col1 >= col2
AND col1 >= col3 THEN col1
WHEN col2 >= col1
AND col2 >= col3 THEN col2
WHEN col3 >= col1
AND col3 >= col2 THEN col3
ELSE col1
END AS Max_number
FROM (VALUES (1,2,3),
(1,2,3),
(1,2,3)) tc (col1, col2, col3)
But things are getting complicated when I want to compare more than 3 columns. Is there any simpler way to do this
Try this Table Value Constructor trick
SELECT (SELECT Max(col)
FROM (VALUES (col1),
(col2),
(col3)) tc(col)) AS Max_number
FROM (VALUES (1,2,3),
(1,2,3),
(1,2,3)) tc (col1, col2, col3)
Additionally it handles NULL values as well
As a note, the logic using case is not as hard as it seems:
SELECT (CASE WHEN col1 >= col2 AND col1 >= col3 THEN col1
WHEN col2 >= col3 THEN col2
ELSE col3
END) AS Max_number
FROM (VALUES (1,2,3),
(1,2,3),
(1,2,3)
) tc (col1, col2, col3) ;
That is, you just need to compare the values in the order they are returned. Once a value is not the maximum, it doesn't need to be part of the comparison. So, for four values:
SELECT (CASE WHEN col0 >= col1 AND col0 >= col2 AND col0 >= col3 THEN col0
WHEN col1 >= col2 AND col1 >= col3 THEN col1
WHEN col2 >= col3 THEN col2
ELSE col3
END) AS Max_number
I prefer the method suggested by VR46, but want to point out that the CASE is not quite as bad as it seems.

Filtering sql data with multiple filter vb.net (winform)

I want to filtering data from SQL to my datagrid.
I have :
1 table (tableX)
3 Columns
Col1 Col2 Col3
1/x/10 BJB 1/20/20
1/y/10 BJB 1/20/30
1/x/10 BJB 1/20/30
1/y/10 BJB 1/20/20
2 datagrid (dg1, dg2)
i want insert :
dg1 with col1 "1/x/10" and col3 "10/20/20"
dg2 with col1 "1/y/10 and col3 "10/20/20
i can filter with only one Col3
"SELECT Col1, Col2, Col3 FROM Tablex WHERE Col3='10/20/20'"
how to filter col1 witch contain "x" or "y" and col3?
==========================================================================
O YEAH.. thanks for the answer.
this for dg1
("SELECT Col1, Col2, Col3 FROM TableX WHERE Col1 like ('%/X/%') AND Col3='10/20/20'")
and this for dg2, the different is just X and Y..lol
("SELECT Col1, Col2, Col3 FROM TableX WHERE Col1 like ('%/Y/%') AND Col3='10/20/20'")
SELECT Col1, Col2, Col3
FROM Tablex
WHERE Col3='10/20/20'
AND (COL1 like '%/x/%' or COL1 like '%/y/%')
SELECT Col1, Col2, Col3
FROM Tablex
WHERE COL1 IN ('1/x/10', '1/y/10')
AND Col3 = '10/20/20
Or you can make the COL1 filter more ambiguous.
SELECT Col1, Col2, Col3
FROM Tablex
WHERE COL1 IN ('%/x/%', '%/y/%')
AND Col3 = '10/20/20