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 2008 Forums
 Transact-SQL (2008)
 Summing different rows...

Author  Topic 

sadderson
Starting Member

13 Posts

Posted - 2009-08-29 : 18:48:31
need to sum different rows for example

Ticket ID1 ID2 value
1 1 null 30
1 2 null 20
1 3 1 10
1 4 2 5
2 1 null 5
2 2 null 10
2 3 1 20
2 4 2 30


Result needs to be (ID2 doesn't matter)
1 1 40
1 2 25
2 1 25
2 2 35


all ID1 + ID2 summed to the value (can be a different name or not)

Thanks

Scott

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-08-29 : 19:48:37
SELECT Ticket, COALESCE(ID2, ID1) ID, Sum(Value) FROM myTable
GROUP BY Ticket, COALESCE(ID2, ID1)


The result you posted for 2,2 is not consistent with the source data you posted and the results for the other values.
Go to Top of Page

sadderson
Starting Member

13 Posts

Posted - 2009-08-29 : 20:05:40
Sorry right 2 2 result should be 40 my mistake..

Go to Top of Page

sadderson
Starting Member

13 Posts

Posted - 2009-08-29 : 20:24:35
Thanks ... works like a charm...

Scott
Go to Top of Page
   

- Advertisement -