I am having trouble writing a case query in SQL. My first question is: is it possible to write an if statement below as a case statement in SQL Query in a select statement?
If not, then please take a look at the case example below and help / advise me to enter a valid format. Thanks and appreciated!
IF (var1 = 1){
do this1;
IF (var 1 = 2){
Do this2;
}Else{
do something else1;
}
Else if (Var 1 = 3){
Do this3;
}Else{
Do something else2;
}
Here is my case study. I know this does not work because it is not a valid case argument. Maybe someone kindly helped me make this a valid expression. Thanks in advance.
SELECT
CASE
WHEN apple.type = 1 OR apple.type = 2
THEN basket.S1
ELSE
CASE
WHEN apple.type = 0 AND basket.S2 is null
THEN basket.S1
ELSE basket.S2
ELSE
CASE
WHEN apple.type = 3 and basket.s3 is null
THEN basket.S1
ELSE basket.S3
END
END
END
FROM .....
WHERE .....
source
share