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

How to use SQL sum if with multiple conditions

| 1dist | 1result | 2dist | 2 result | 3dist | 3result|
|-------|---------|-------|----------|-------|--------|
|  5    |  MADE   |  NULL |   NULL   |  NULL | NULL   |
| 10    |  MISS   |  2    |   MADE   |  NULL | NULL   |
|  5    |  MADE   |  NULL |   NULL   |  NULL | NULL   |
| 20    |  MISS   |  5    |   MILL   |  2    | MADE   |

From this table I’m looking to sum the 1dist, 2dist, 3dist where the result columns = made.
The result I’m looking for in the example would be 5+2+5+2 = 14

SELECT SUM(CASE WHEN 1result = MADE THEN 1dist) +
       SUM(CASE WHEN 2result = MADE THEN 2dist) +
       SUM(CASE WHEN 3result = MADE THEN 3dist)
FROM table

>Solution :

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

Try This

SELECT SUM(CASE WHEN 1result = 'MADE' THEN 1dist END) +
SUM(CASE WHEN 2result = 'MADE' THEN 2dist END) +
SUM(CASE WHEN 3result = 'MADE' THEN 3dist END)  As Final_Result
FROM Table_Name
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