1

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
2
  • Are these all columns in a table that you're querying? Commented Dec 2, 2021 at 12:30
  • @Mureinik : Yes all are their in table : employee Commented Dec 2, 2021 at 12:31

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks tim let me try it
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.