Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL question on SUM and DISTINCT - Need help

Author  Topic 

rajabadsha
Starting Member

4 Posts

Posted - 2007-12-07 : 07:39:18
I have a table named 'MyTable'. There are two columns: NUM, AVERAGE. The table has data as follows:
NUM AVERAGE
1 5
2 15
2 10
2 5
4 2
4 8
5 10
6 7
6 7

I want to write a query that will keep the distinct number from the first column and will generate the sum for each distinct number from the second column. The result will be as following:
1 5
2 30
4 10
5 10
6 14

Can anyone help

Ifor
Aged Yak Warrior

700 Posts

Posted - 2007-12-07 : 07:44:21
Read up on GROUP BY and SUM in BOL (Books online)
Go to Top of Page

kiruthika
Yak Posting Veteran

67 Posts

Posted - 2007-12-08 : 01:03:45
hi!

select num,sum(average) as AverageVal from mytable group by num

kiruthika!
http://www.ictned.eu

Go to Top of Page
   

- Advertisement -