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')
then
empnfees=(empvar / empdar) * empcost
else if depcost <> empcost and empcourseid <> 'A'
then
empnfees=empcost
else
empnfees=depcost
CodePudding user response:
Using a CASE
expression, we can try:
CASE WHEN depcost <> empcost AND empcourseid IN ('A', 'B')
THEN (empvar / empdar) * empcost
WHEN depcost <> empcost AND empcourseid <> 'A'
THEN empcost
ELSE depcost END AS empnfees
CodePudding user response:
If you can have NULL
values then (as NULL <> something
is never true):
CASE
WHEN ( depcost <> empcost
OR (depcost IS NULL AND empcost IS NOT NULL)
OR (depcost IS NOT NULL AND empcost IS NULL)
)
AND empcourseid in ('A','B')
THEN empvar / empdar * empcost
WHEN ( depcost <> empcost
OR (depcost IS NULL AND empcost IS NOT NULL)
OR (depcost IS NOT NULL AND empcost IS NULL)
)
AND ( empcourseid <> 'A'
OR empcourseid IS NULL
)
THEN empcost
ELSE depcost
END AS empnfees