Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3799164
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:41:22+00:00 2026-05-19T13:41:22+00:00

Can someone tell me why this isn’t working? I’d like to get the sum

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T13:41:23+00:00Added an answer on May 19, 2026 at 1:41 pm

    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:

    SUM(col1) + SUM(col2) AS total
    

    If one of the columns could be entirely NULL this still won’t work. Then you can try this:

    SUM(IFNULL(col1, 0)) + IFNULL(col2, 0)) AS total
    

    For example, imagine you have this table:

    col1 | col2
    -----+----
    5    | NULL
    5    | 10
    

    Then try these different queries:

    SELECT SUM(col1), SUM(col2)        FROM yourtable;    -- Returns 10, 10
    SELECT SUM(col1 + col2)            FROM yourtable;    -- Returns 15
    SELECT SUM(col1) + SUM(col2)       FROM yourtable;    -- Returns 20 
    SELECT SUM(col1 + IFNULL(col2, 0)) FROM yourtable;    -- Returns 20 
    

    Test data:

    CREATE TABLE yourtable (col1 INT NOT NULL, col2 INT NULL);
    INSERT INTO yourtable (col1, col2) VALUES (5, NULL), (5, 10);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please tell me why this isn't working? I would like to be
Can someone please tell me why this isn't working? var items = <select id=\orderer_search\
can someone please tell me why this isn't working? For some reason I need
Please can someone tell me why this isn't working before I defenestrate everything on
I'm trying to run this code, but initialize() isn't called. http://jsfiddle.net/r3sA8/ Can someone tell
Can someone tell me why this code isn't removing the subview that's being animated?
Can someone tell me why this processes all the files and then does it
Can someone tell me why this http://hupcapstudios.com/projects/calendar.html won't load in IE? The error message:
Can someone tell me why this unit test that checks for exceptions fails? Obviously
Can someone tell me where this structure language is from? I am not familiar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.