1

I am getting incorrect results when I try to insert a SUM into an IF clause. The results are correct when I use COUNT, either incorrect or NULL when I use SUM. I have been able to produce the correct results for each statement through another query (as a means of validating the formula). What is the syntax to get correct results for an SUM within an IF statement? Based on another StackOverflow question, I attempted to fix the formula, but it produced an error.

SELECT
IF (Artist LIKE '%Hillsong%' , 'Hillsong', NULL ) as Artist,

COUNT(IF(CCD BETWEEN 28 AND 730,1,NULL)) AS 1_CC,    -- result 191, which is correct 
IF(CCD BETWEEN 28 AND 730, SUM(CC28),NULL) AS 2_CC,  -- result NULL, should be 610   xx
COUNT(IF(CCD > 28,1,NULL)) AS 3_CC,                  -- result 684, which is correct 
COUNT(IF(CCD < 730,1,NULL)) AS 4_CC,                 -- result 502, which is correct 
IF(CCD > 28,SUM(CC28),NULL) AS 5_CC,                 -- result 2253, should be 1882  xx
--- SUM(IF(CCD > 28,CC28,NULL) AS 6_CC,              -- my attempt to fix, creates error
IF(CCD < 730,SUM(CC28),NULL) AS 7_CC                 -- result NULL, shoul be 981    xx

FROM praisecharts_reporting.large_sales_report
GROUP BY 1;

As a frame of reference, I am a music publisher, and I am trying to get results for all songs where the artist includes "Hillsong", where the Chord Chart (CC) has been available between 28 and 730 days (CCD BETWEEN 28 AND 730). The COUNT should tell me how many song titles qualify, and SUM should tell me the total unit sales for all songs that qualify.

3 Answers 3

1

I figured out the answer to my problem. The syntax for putting an IF clause inside a SUM is to use CASE WHEN. Below is the solution to my query above, and it produces correct results all around:

SELECT
IF (Artist LIKE '%Hillsong%' , 'Hillsong', NULL ) as Artist,

COUNT(IF(CCD BETWEEN 28 AND 730,1,NULL)) AS 1_CC,
SUM(CASE WHEN CCD BETWEEN 28 AND 730 THEN CC28 ELSE 0 END) AS 2_CC,
COUNT(IF(CCD > 28,1,NULL)) AS c3_CC,  
COUNT(IF(CCD < 730,1,NULL)) AS c4_CC, 
SUM(CASE WHEN CCD > 28 THEN CC28 ELSE 0 END) AS 5_CC,
SUM(CASE WHEN CCD < 730 THEN CC28 ELSE 0 END) AS 6_CC

FROM praisecharts_reporting.large_sales_report
GROUP BY 1;
Sign up to request clarification or add additional context in comments.

3 Comments

The syntax for putting IF clause inside SUM is SUM(IF(condition, result_if_true, result_if_false)), you are just missing a closing parenthesis in your original query. Your SUM - CASE is correct as well. But I disagree with your statement: The syntax for putting an IF clause inside a SUM is to use CASE WHEN
OK, I will use the code that you have suggested. It is simpler, and it works just the same. I have tested it now with < > as well as BETWEEN, and all seem to work.
+ 1 for finding alternatives, we can always find other things that will work if what we have doesn't work :)
0

You should be on the right track on your attempt to use SUM - IF

--- SUM(IF(CCD > 28,CC28,NULL) AS 6_CC,              -- my attempt to fix, creates error

If you look closely, you are missing an ending parenthesis. We must pay attention to the error we received. Most likely you received a syntax error right after AS 6_CC,.

Just add a closing parenthesis after NULL:

SUM(IF(CCD > 28,CC28,NULL)) AS 6_CC, 

Try to use SUM - IF on your other columns as well. Let me know if this works.

With SUM - IF you can have a true / false result, which is as simple as it can be (though you can have nested if's inside, but that would make it unreadable).

With SUM - CASE you have an option to have more results by providing more conditions, just like a SWITCH statement.

4 Comments

Did you notice that I was able to get accurate results with the CASE WHEN logic? Can you check my latest ANSWER post and see if that looks good to you?
@RyanDahl they are both correct. SUM - IF was your initial approach and I don't want you to get mislead that it does not work. SUM - IF works and SUM - CASE as well.
Yes, you are right. Just tried it. Thank you very much! Always good to have two options :-)
No problem mate.
0

Without the underlying data it would be hard to verify the results. But your NULL result may be due to a underlying NULL in data or due to condition clause.

Try replacing SUM(CC28) with SUM(IFNULL(CC28,0))

A sum of Integer and NULL equals NULL. Hence you might be getting NULL in second set.

4 Comments

If this is what you mean, it creates an error: IF(CCD BETWEEN 28 AND 730, SUM(IFNULL(CC28,0),0) AS s1_CC
This is creating NULL for me: IF(CCD < 730,SUM(CC28),NULL) AS s5_CC. I know that there are 502 that qualify as CCD < 730. Out of those 502, I want to add up all the values of CC28. The total should be 981. If any CCD fields are > 730, they should count as NULL. That's what I think the formula is supposed to do, but it isn't calculating correctly.
This also produces an error: IF(CCD > 28,SUM(IFNULL(CC28,0),0) AS s4_CC
Glad that you were able to resolve by Case When. That will also convert your integer to 0 for any unmatched conditions. IFNULL syntax for MySql - dev.mysql.com/doc/refman/5.7/en/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.