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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Need help with aggregating fields

Author  Topic 

herman404
Starting Member

10 Posts

Posted - 2006-07-29 : 10:00:47
Hi everyone, I have some trouble writing a SQL stored procedure that can do the following:

We have data in one table in numeric form, but we want to sum the data in this table based on the values of two different alpha fields. To illustrate, let me write the following example:

Table with these records:
A B 1.1 2.2 Blah1 Blah1
A B 2.3 5.6 Blah2 Blah2
B C 7.8 9.1 Blah3 Blah3
B C 4.5 1.0 Blah4 Blah4
R F 1.1 4.3 Blah5 Blah5
B A 3.1 2.7 Blah6 Blah6

I need to write a query that will return the following result set from the above table:
A B 3.4 7.8 Blah1 Blah1
B C 12.3 10.1 Blah3 Blah3
R F 1.1 4.3 Blah5 Blah5
B A 3.1 2.7 Blah6 Blah6

If the alphanumberic keys are the same, the sum the numeric columns up whilst displaying one one of the records, the blah fields don't matter if only one is displayed. Can anyone recommend the best way to do this?
I'm running MS SQL 2005.

Thanks!

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-07-29 : 10:53:48
Select col1,col2,sum(col3),sum(col4),min(col5),min(col6) from yourTable
Group by col1,col2

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -