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 > SQL statement to sum rows, not columns

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-20-04, 11:19
dbHell dbHell is offline
Registered User
 
Join Date: Mar 2004
Posts: 5
SQL statement to sum rows, not columns

Hi all,

I need to sum and average the values in each record so that I can use it to base my queries on.

ie:

I have a record with four values: 1, 3, 5, 7

The average is 4.


If I want to extract all records from a table where the average of these numbers is greater than 4, what would my statement look like?

Assume the values for the individual fields are: Value1, Value2, Value3, Value4. The table name is Total.



All built-in functions work on the columns over several records. My issue is different. I want something to work on several columns (fields) of one record.

Thanks in advance
Reply With Quote
  #2 (permalink)  
Old 04-20-04, 11:30
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
With only the information you've provided, the best I can do is:
PHP Code:
SELECT *
   
FROM totals
   WHERE  4 
< (Coalesce(Value10) + Coalesce(Value20)
+    
Coalesce(Value30) + Coalesce(Value40)) / 
When working with columns within a row, you can easily derive the formulas for things like an average. The aggregate functions are provided for more difficult cases where you have arbitrary numbers of rows.

-PatP
Reply With Quote
  #3 (permalink)  
Old 04-26-04, 16:21
joebednarz joebednarz is offline
Registered User
 
Join Date: Dec 2003
Location: Oklahoma, USA
Posts: 354
Code:
SELECT * FROM total
WHERE ( value1 + value2 + value3 + value4 ) / 4 > 4;
JoeB
Reply With Quote
  #4 (permalink)  
Old 04-26-04, 16:42
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
much prefer pat's solution

defensive sql

and guys, both of ya get minus 10 marks for the evil dreaded "select star"
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 04-26-04, 22:00
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Quote:
Originally Posted by r937
much prefer pat's solution

defensive sql

and guys, both of ya get minus 10 marks for the evil dreaded "select star"
Sorry, you can't take points away unless the specs contain enough information to avoid the problem. We only got the names of the columns of interest, no posted DDL, so we had to use * to ensure a complete answer!

-PatP
Reply With Quote
  #6 (permalink)  
Old 04-26-04, 22:18
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
the preferred solution in that case is to use foo, bar, bat, qux and/or as many of those as necessary to make your point about the syntax

anything but the dreaded asterisk
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 04-26-04, 22:28
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Quote:
Originally Posted by r937
the preferred solution in that case is to use foo, bar, bat, qux and/or as many of those as necessary to make your point about the syntax

anything but the dreaded asterisk
Foo and bar I know all too well... Bat is a flying critter, or a file extension for a badly crippled scripting language. What the frog is a qux ???

-PatP
Reply With Quote
  #8 (permalink)  
Old 04-26-04, 22:39
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
there's a whole flock of 'em

i had bat where i shoulda had baz

foo is
Quote:
First on the standard list of metasyntactic variables used in syntax examples. See also bar, baz, qux, quux, garply, waldo, fred, plugh, xyzzy, thud.
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Last edited by r937; 04-26-04 at 22:41.
Reply With Quote
  #9 (permalink)  
Old 04-26-04, 22:51
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Well I'll be furshlurgled (to borrow one from Don Martin)!

I done has learnt sumpin' Now I are gonna go to home!

-PatP
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