Nested case statement sql - mysql

I'm having issues with writing a case statement in SQL. My first question is: Is it possible to write the if statement below as a case statement in a SQL Query within the select statement?
If no, then please have a look at the case statement below and help/guide me to get into a valid format. Thanks and much 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 statement. I know it doesn't work because it's not a valid case statement. Could someone kindly help me in making it a valid case statement. 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 .....

I think you are over complicating your case statement , looking at your first example you case statement should be fairly simple,
something like ....
SELECT CASE
WHEN #Var1 = 1 THEN 'Something 1'
WHEN #Var1 = 2 THEN 'Something 2'
WHEN #Var1 = 3 THEN 'Something 3'
ELSE 'Something Else'
END
FROM .....
WHERE .....
You case statement can we written something like this...
SELECT
CASE
WHEN apple.[type] IN (1,2)
THEN basket.S1
WHEN apple.type = 0 AND basket.S2 is null
THEN basket.S1
WHEN apple.type = 3 and basket.s3 is null
THEN basket.S1
ELSE basket.S3
END

Since you're checking for nulls and substituting non-null values, you can make the query shorter by using the COALESCE function.
SELECT
CASE
WHEN apple.type IN (1, 2) THEN basket.s1
WHEN apple.type = 0 THEN COALESCE(basket.s2, basket.s1)
WHEN apple.type = 3 THEN COALESCE(basket.s3, basket.s1)
END
FROM ...

Related

CASE Statement MYSQL with Condition

I got 2 CASE statements: First CASE statement is as follows:
Case When ((cust_shipmentdate_awb Is Null OR cust_shipmentdate_awb = '')
AND (comp_shipdate_awb IS NULL OR comp_shipdate_awb = '')) Then 'Pending'
ELSE 'Shipped' End AS shipment_status
The Second CASE statement is as follows:
Case When apbg_bal_pay_amt ='0' Then 'Received'
Else 'Pending' End AS payment_status
Iam looking to write one more CASE statement named OVERALL_Status. That is basically a combination of both this CASES (shipment_status and payment_status), which means if Shipment status is 'Shipped' AND Payment_Status is 'Received' then Overall_status is 'Completed' else 'Not Completed'. Can anyone please help me on this. I am really struck here. I tried the combination of both the CASES, but not working:
Case When (cust_shipmentdate_awb Is Null OR cust_shipmentdate_awb = '') AND
(comp_shipdate_awb IS NULL OR comp_shipdate_awb = '') AND (apbg_bal_pay_amt != '0') Then
'Pending' ELSE 'Completed' End AS overall_status
There is two solutions as I see it:
1. You make the current query a subquery and add another CASE statement on the result of the subquery. (the pretty solution as I see it)
2. You add another WHEN to the existing case. Combining the clauses in the first two WHEN clauses to get the result. (probably the most optimized solution)
How about this?
(case when (cust_shipmentdate_awb Is Null OR cust_shipmentdate_awb = '') and
(comp_shipdate_awb IS NULL OR comp_shipdate_awb = '')
then 'Not Completed'
when apbg_bal_pay_amt = '0' Then 'Completed'
else 'Not Completed'
end) as overall_status

If Else condition in mysql to check null

How should I make if condition in mysql where time = null then I want result in NO else YES?
SELECT
case when TIME_TO_SEC(TIMEDIFF(Event01.EndTime ,Event01.StartTime)) != NULL THEN
#Loss:='YES'
ELSE
#Loss:='NO'
END AS Loss
from Event01,(SELECT #Loss:= 0) AS Loss
Query
SET #Loss := 0;
SELECT
#Loss:= case when TIME_TO_SEC(TIMEDIFF(Event01.EndTime ,Event01.StartTime)) IS NOT NULL
THEN 'YES'
ELSE 'NO' END
FROM Event01;
Try this, You have to check NULL using is clause.
SELECT
case when TIME_TO_SEC(TIMEDIFF(Event01.EndTime ,Event01.StartTime)) is not NULL THEN
'YES'
ELSE
'NO'
END AS Loss
from Event01
Hope it will help.
Try this
SELECT case when TIME_TO_SEC(TIMEDIFF(Event01.EndTime ,Event01.StartTime)) IS NULL THEN 'NO' ELSE 'YES' END AS Loss from Event01

using if statement in mysql query

I would like to use if statement in sql query :
what I want :
if(tractions_delivery.send_date_id !=0 ){
date_send_commodities.id = tractions_delivery.send_date_id
}
my query :
from
tractions_delivery,user_address,province,city,date_send_commodities,users
WHERE
tractions_delivery.tr_id = $tr_id
AND
tractions_delivery.address_id = user_address.id
AND
user_address.province_id = province.id
AND
user_address.city_id = city.id
AND
//not work
(tractions_delivery.send_date_id IS NOT 0 date_send_commodities.id = tractions_delivery.send_date_id)
AND
users.id = user_address.user_id
You could use the CASE-statement
SELECT
*
FROM
tractions_delivery,
user_address,
province,
city,
date_send_commodities,users
WHERE
tractions_delivery.tr_id = $tr_id AND
tractions_delivery.address_id = user_address.id AND
user_address.province_id = province.id AND
user_address.city_id = city.id AND
CASE WHEN tractions_delivery.send_date_id != 0 THEN date_send_commodities.id = tractions_delivery.send_date_id ELSE 1=1 END AND
users.id = user_address.user_id
You can only use if statements in stored procedures or functions. If you just write a sql statement unfortunately you cannot use if statements around the query. But you can use logic in the query itself, e.g.:
SELECT CASE WHEN col1 = col2 THEN'col1 equals col2' else 'col1 doesnt equal col2' ELSE
FROM table1
So around doesnt work, but in the field list you can create CASE WHEN ELSE END logic.
CASE or IF() operators can be of help.
Examples,
SELECT (CASE 1 WHEN 1 THEN 'One' WHEN 2 THEN 'Two' ELSE 'More' END) 'Result';
OR
SELECT IF(1=1, 'One', 'Two') 'Result';
These CASE and IF() operators can be used in the SELECT clause to conditionally interpret column values and return in the resultset.
Note: Do not confuse CASE operator here with 'CASE conditional syntax block' that ends with END CASE.

Select * from table WHERE `column` = x if `column` != -1

I need to make a select like,
Select * from table WHERE column = x if column != -1
but i have no idea for now.
Anyone know or made in past something like that?
Thanks.
You should also write like this,
Select * from table
WHERE
1 = case when column != -1 then
case when column = x then 1 else 0 end
else 1 end
You can utilize case when in where clause.
Similarly you can add more conditional criteria like,
Select * from table
WHERE
1 = case when column != -1 then
case when column = x then 1 else 0 end
else 1 end
AND
1 = case when column1 [conditional operator] value then
case when column1 = xx then 1 else 0 end
else 1 end
This is just an example how you can integrate more conditional criteria together, even though you can add more case when in else part even.

using nested case condition in mysql query

if(qtype==3)
column=somecolumn;
else if(qtype==2)
{
if(open==0)
column=anycolumn;
else
column =somecolumn;
}
else{
acolumn,bcolumn,ccolumn //this else----|
} |
----------------|
I want to achieve the above nested if-else condition in my mysql query.where the condition will be based on one of the column.this is what i have done so far.
select qtype,open,(Case when qtype=2 then answer
Else (Case when qtype=3 then
(Case when open=0 then somecolumn
Else othercolumn End) End)
Else....//how to implement this else here)
i am in confusion how to integrate the last else part?
Thanks
The format is correct, but you switch the 2 and 3 compared to the if-else psuedocode at the begining:
select qtype,open,(Case when qtype=3 then answer
Else (Case when qtype=2 then
(Case when open=0 then somecolumn
Else othercolumn End) End))
Looking at the case syntax, here is what I came up with.
CASE qtype
WHEN 3 THEN column=somecolumn;
WHEN 2 THEN (CASE open WHEN 0 THEN column=anycolumn ELSE column = somecolumn)
ELSE othercolumn
END CASE