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
CodePudding user response:
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
Edit: as suggested by @JonasMetzler, an ELSE would also do the job:
SELECT CASE F
WHEN 'a particular value' THEN A B C
ELSE A B C D
END AS E
FROM YourTable
And since there seem to be just 2 options for F value to evaluate ('being a certain value' and 'not being it'), we could also use an IIF statement:
SELECT IIF(F = 'a certain value', A B C, A B C D) AS E
FROM YourTable
and in the three examples, you could extract the A B C part since they are always added:
SELECT IIF(F = 'a certain value', 0, D) A B C AS E
FROM YourTable