Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading