If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > How to sum multiple fields together

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-30-04, 17:31
mlscw mlscw is offline
Registered User
 
Join Date: Sep 2003
Posts: 4
Question 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?
Reply With Quote
  #2 (permalink)  
Old 08-31-04, 09:22
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
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
Reply With Quote
  #3 (permalink)  
Old 08-31-04, 15:17
mlscw mlscw is offline
Registered User
 
Join Date: Sep 2003
Posts: 4
Smile Thank you for your response

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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On