I am little bit stuck need to write the SQL if else if for below logic
As only basic knowledge on SQL
Below logic need to derived to SQL query
if depcost <> empcost and empcourseid in ('A','B') and empcd='LTR'
then
empnfees=(empvar / empdar) * empcost
else if depcost <> empcost and empcourseid <> 'A'
then
empnfees=empcost
else
if empcd='XYZ'
empnfees=empcost
else
empnfees=depcost
LOGIC :
CASE WHEN depcost <> empcost AND empcourseid IN ('A', 'B') and empcd ='LTR'
THEN (empvar / empdar) * empcost
WHEN depcost <> empcost AND empcourseid <> 'A'
THEN empcost
ELSE
CASE
when empcd='XYZ'
then empcost
else
depcost
END as EMPVAR
The above code is throwing me error : missing keyword How to resolve it ?
CodePudding user response:
You use two cases but only one end. You need another end here:
CASE WHEN depcost <> empcost AND empcourseid IN ('A', 'B') and empcd ='LTR'
THEN (empvar / empdar) * empcost
WHEN depcost <> empcost AND empcourseid <> 'A'
THEN empcost
ELSE
CASE
when empcd='XYZ'
then empcost
ELSE
depcost
END --------------- missing end
END as EMPVAR
Hope I helped you