Changing query based on record value

So I have this table to see usage report,
Let’s say column A B C D E F

if column F has a particular value
I want to
A + B + C = E
else I wanted
A + B + C + D = E

My brain can’t handle the logic to be it in SQL query
Do I need to use IF ELSE or DECLARE or CAST or idk

>Solution :

Here is a possible way to do this; anyway I’d suggest to have a look to the SELECT CASE documentation since it’s not that complicated (I assumed you’re using sql server):

SELECT CASE F 
WHEN 'a particular value' THEN A + B + C 
WHEN 'not a particular value' THEN A + B + C + D
END AS E
FROM YourTable

Leave a Reply