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

Is there any way to write a case statement in sql, if column A >0 then column B, Column C= 0

I’ve multiple columns which has the same value and it is showing incorrect results when I summarize them. Is there any way to write a case statement in sql, if column A >0 then make all remaining columns as 0 (column B, Column C, Column D= 0)

if column B >0 then make all remaining columns as 0 (column A, Column C, Column D= 0)

1st column gets the higher priority in this sequence.

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

enter image description here

enter image description here

I tried writing case statement but it did not work.

>Solution :

See if this example helps:

SELECT a
    ,CASE WHEN a > 0 THEN 0 ELSE b END AS b
    ,CASE WHEN a > 0 OR b > 0 THEN 0 ELSE c END AS c
    ,CASE WHEN a > 0 OR b > 0 OR c > 0 THEN 0 ELSE d END AS d
FROM tbl;
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