Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2003
    Posts
    4

    Question Unanswered: How to sum multiple fields together

    Hi everyone

    I am still learning by the "Trial by Fire" method. Thanks for all previous help!

    Table name: Class_1

    Field names: Rating_5; Rating_4; Rating_3; Rating_2; Rating_1; Rating; 0; Not_Rated

    Is this the correct SQL way to sum up one field: Select sum(Rating_5) from Class_1

    How would I sum each field and then add the sums together?

    Also what is Firebird SQL?

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Yes, that is the correct way to sum one column.

    If the columns don't allow NULL values, you can just add the sums together. If they do allow NULL values, you have to be a bit more defensive, something like:
    Code:
    SELECT Sum(rating1) AS rating1
    ,  Sum(rating2) AS rating2
    ,  Sum(rating3) AS rating3
    ,  Sum(rating4) AS rating4
    ,  Sum(rating5) AS rating5
    ,  Coalesce(Sum(rating1), 0)
    +  Coalesce(Sum(rating2), 0)
    +  Coalesce(Sum(rating3), 0)
    +  Coalesce(Sum(rating4), 0)
    +  Coalesce(Sum(rating5), 0) AS all_ratings
       FROM Class_1
    -PatP

  3. #3
    Join Date
    Sep 2003
    Posts
    4

    Smile Thank you for your response

    Thank you for your response. I will give it a go.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •