SQL Server: SELECT IF conditionally - sql

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;

Related

SQL union grouped by rows

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

oracle sql group by column which count them

I ran below query to fetch count of data which I can see in output but it does not working as I wish
How can I print count of col6 & col7 in output?
Am I clear?
select col1, col2,
col3, col4, decode(col5,'S','Success','F','Failed'), col6, col7, count(*)
from mytable
where col1 in (select FIELD1 from temp)
and col8 = 4
group by col1, col2, col3,col4,col5,col6,col7
See if this works
select count(col6), count(col7)
from mytable
where col1 in (select FIELD1 from temp)
and col8 = 4;
You need to use the proper aggregate function and remove the col6 and col7 from GROUP BY clause following query:
select col1, col2, col3, col4, decode(col5,'S','Success','F','Failed'),
count(col6), count(col7), count(*) -- used count for col6 and col7
from mytable
where col1 in (select FIELD1 from temp)
and col8 = 4
group by col1, col2, col3,col4,col5 -- removed col6 and col7 from here

How to use a variable as a select statement and then sum multiple values in a variable?

I have multiple rows with the following columns:
col1
col2
col3
col4
I want to say if rows where col3 equals col2 of other rows where col1 equals 'a111', then sum col4 of the rows where col3 equals col2 of other rows where col1 equals 'a111', and then rename the sum column to "Total".
Example table with the four columns and four rows:
col1 col2 col3 col4
---- ---- ---- ----
a222 a333 4444
a111 a333
a555 a444 1111
a111 a444
I've tried the following but it does not work.
Declare
var1 = Select col2 from table1 where col1='a111';
var2 = Select col3 from table1 where col3=var1;
var3 = Select col4 from table1 where col3=var1;
Begin
If var2=var1
Then Select SUM(var3) As "Total";
End
Expected result is:
Total
5555
I do not have the strongest of knowledge in programming overall or Oracle. Please ask any questions and I will do my best to answer.
Your logic is convoluted and hard to follow without an example of the data you have and an example of the data you want.. but translating your pseudocode into sql gives:
Declare
var1 = Select col2 from table1 where col1='[table2.col2 value]';
Called "find" in my query
var2 = Select col3 from table1 where col3=var1;
var3 = Select col4 from table1 where col3=var1;
Achieved by joining the table back to the "find"
Begin
If var2=var1
Then Select SUM(var3) As "Total";
End
Achieved with a sum of var3 on only rows where var1=var2, in "ifpart"
SELECT SUM(var3) FROM
(
SELECT alsot1.col3 as var2, alsot1.col4 as var3
FROM
table1 alsot1
INNER JOIN
(
SELECT t1.col2 as var1
FROM table1 t1 INNER JOIN table2 t2 ON t1.col1 = t2.col2
) find
ON find.var1 = alsot1.col3
) ifpart
WHERE
var1 = var2
This could be simplified, but I present it like this because it matches your understanding of the problem. The query optimizer will rewrite it anyway when the time comes to run it so it only pays to start messing with how it's done if performance is poor
By the way, you clearly said that two tables join via a common named col2 but you then in your pseudocode said the tables join on col1=col2. I followed your pseudocode
This sounds like something that hierarchical queries could handle. E.g. something like:
WITH your_table AS (SELECT NULL col1, 'a222' col2, 'a333' col3, 4444 col4 FROM dual UNION ALL
SELECT 'a111' col1, 'a333' col2, NULL col3, NULL col4 FROM dual UNION ALL
SELECT NULL col1, 'a555' col2, 'a444' col3, 1111 col4 FROM dual UNION ALL
SELECT 'a111' col1, 'a444' col2, NULL col3, NULL col4 FROM dual UNION ALL
SELECT 'a666' col1, 'a888' col2, NULL col3, NULL col4 FROM dual UNION ALL
SELECT NULL col1, 'a777' col2, 'a888' col3, 7777 col4 FROM dual)
SELECT col1,
SUM(col4) col4_total
FROM (SELECT connect_by_root(col1) col1,
col4
FROM your_table
CONNECT BY col3 = PRIOR col2
START WITH col1 IS NOT NULL) -- start with col1 = 'a111')
GROUP BY col1;
COL1 COL4_TOTAL
---- ----------
a666 7777
a111 5555
Nevermind. I believe I've determined the answer myself. I over complicated what I wanted. Thank you anyway.
Answer:
Select Sum(col4) as "Total" from table1 where col3 in (Select col2 from table1 where col1='a111')

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

SQL Query select col1, col2, if col3='xx' then 'yy' else col3, col4 col5??? is it posible?

well i want to do a query as this but i dont know if it is posible
Select
col1,
col2,
col3,
if(contain(col3,'somethingx')) then 'hello' else 'world' as col4,
col5
from table1
In other words, I want to select
col1
col2
col3
if col3 contains the word 'somethingx' then I want to select 'hello' as col4
Otherwise I want to select 'world' as col5 from my table.
SELECT
col1,
col2,
col3,
CASE WHEN col3 LIKE '%somethingx%' THEN 'hello' ELSE 'world' END AS col4,
col5
FROM table1