Can someone tell me why this isn’t working? I’d like to get the sum of two different columns and add them together. The test data is 10 in one column and 10 in the other, totaling 20 which is what I’m expecting.
Sum(col1 + col2) as total
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Probably you have some NULL values in one or both of the columns. The aggregate function SUM ignores NULL values, but the addition operator does not – the value of (1 + NULL) is NULL (not 1 as you might expect). As a result the sum will be lower than expected.
To get the correct sum, you can SUM over each column separately and add the results:
If one of the columns could be entirely NULL this still won’t work. Then you can try this:
For example, imagine you have this table:
Then try these different queries:
Test data: