How multiple CASE WHEN scenario works in SQL? - sql

If in a query we have 2 case scenario satisfying the condition, which one will be picked by the SQL to show the output ?
sample Query :
SELECT
CASE
WHEN field1 = 'a' THEN 1
WHEN field2 = 'a' THEN 2
ELSE 3
END
from table ;

In SQL they will be evaluated in the order they are written so the first match wins.

As soon as a condition is true within your case conditions it will ignore all the later conditions.
If field1='a' then it will return 1 even if field2='a'. But if field1<>'a' and field2='a' then it will return 2 and only if field1 and field2 are both not 'a' then it will return 3.
I am assuming that field1 and field2 are column names.

Related

SQL filter 3 columns of a table

I have a table as below:
Original table
I want to use SQL to filter out any records Type= 'B' or any records ID=1 or any records Color='red'.
I can filter out step by step.
Could I do it once.
I tried the Where Type <> 'B' OR ID <> 1 OR Color <> 'red' in SQL Server. But it doesn't work. Could someone help?
Thanks!
The expected result should be:
What you are saying, in SQL:
...
WHERE
NOT (TYPE='B'
OR ID=1
OR Color='red')
or, applying the NOT operator to the parenthesised section:
...
WHERE
TYPE<>'B'
AND ID<>1
AND Color<>'red'

Using case statement to compare columns

I'd like to use a case statement to compare multiple rows on 2 columns. For example, if row 1 column 1 and row 2 column 1 match but row 1 column 2 and row 2 column 2 don't, then xx. I have
CASE
WHEN (pprof.description = pprof.description and PCtrl.[sequence] <> PCtrl.[sequence])
THEN xx
but that doesn't return any values, which I know to be incorrect. I'm new to SQL so apologies if I've got this all wrong.
Edit:
Here's some sample data:
Column 1
Column 2
Column 3
123
-A
-No
123
-B
-Yes
Can't figure out the formatting here but there are 3 columns of data above. I'd like the case statement to evaluate whether column 1 match in 2 different rows (i.e., 123 = 123) and also whether column 2 doesn't (A <> B) and if both those conditions are true, return a value in column 3 (in my case, make the No a Yes, since 123-B is Yes). It might be worth noting that the "Yes" and "No" themselves are built into the larger case statement here:
(CASE WHEN tenure.description not in ('casual','co-op','fswep') THEN CASE WHEN (pprof.description = pprof.description and PCtrl.[sequence] <> PCtrl.[sequence])
THEN CASE WHEN (PEmp.Employee_Number = PEmp3.Supervisor_Number) THEN 'Yes' ELSE 'No' END END END) as 'People Manager'
You will want to do a self join here. Without seeing your data, I can't really give you an answer, but you want something like this.
Update A
Set column_3 = 'YES' /* or put CASE statement here /*
FROM pprof A
INNER JOIN pprof B
ON a.description = b.description
AND a.sequence != b.sequence
You may need more join conditions depending on the form of your data and what you want.

How to sort combination of integer and text in PostgreSQL?

I have this table
id value
1 OK
2 xminimum
3 NO
4 YES
I want to sort this table by value where minimum is always first then the rest according to alphabetic order of value column
Meaning:
xminimum
NO
OK
YES
I wrote this query:
Select *
from table_a
order by case when value='xminimum' then 1 else ????? end
I don't know what to put in the else... conceptually it should be else value end so it means alphabetic order.. but I can not combine integer with text.
How do I fix it?
As requested, copied from my comment:
Select *
from table_a
order by case when value='xminimum' then 1 else 2 end, value
Another solution:
SELECT *
FROM table_a
ORDER BY value <> 'xminimum', value;
Do it like you have and add the value column as second column to sort by:
SELECT *
FROM table_a
ORDER BY CASE WHEN value='xminimum' THEN 1 ELSE 2 END, value

Can a CASE expression have 2 resultant values

I have to write a case expression in SQL which goes like this,
case condition
if (T_CD = 'Y')
Case C_CD = 'H3'
set R_ID = 3 and RS_ID = 25
CASE A_FLG = 'N' and Mod = 'D'
set R_ID = 3 and RS_ID = 31
Both R_ID and RS_ID populate columns in a different table and have to be derived as per condition above.
My question is - Since I want 2 separate fields out of my case expression, will a single Case give out 2 resultant field values for me. Or Do I have to write 2 different case expressions for it.
If your dbms supports row types, maybe this works for you:
select case when a = 1 then (1,2) else (3,4) end from testtable;
The SQL Validator says:
The following feature outside Core SQL-2003 is used:
T051, "Row types"

Where clause based on priority

Consider Following table named A:
State City Rank
S C 1
AB C1 2
* C2 3
I want to select all columns such that
If State is AB return all such rows
If condition 1 is not met return all rows with state *. If condition 1 is met don't look on to this condition
As per above example I should get row 2. I tried several things like
select state
case when a.state = 'AB' then 1
when a.state = '*' then 2
end as priority from A where state in ('AB','*') order by priority
But above query returns more than one rows. I want exact one row which matches above condition.
Please help
EDIT1:
I want to avoid sub queries due to performance issues.
Try this:
select * from A
where state=case
when exists(select * from A where state='AB') then 'AB'
else '*'
end
Here is the SQL Fiddle demonstrating the above.