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
 sum the monthly data to annual total

Author  Topic 

goligol
Posting Yak Master

128 Posts

Posted - 2011-10-17 : 11:23:22
Hi,

I have the following table,

id dist MM YYYYMM val1 val2 val3 val4 val5
45 39 10 200710 0 0 0 43730 30530
45 39 11 200711 890830 890830 4883 934560 921360
45 39 12 200712 0 0 0 934560 921360
46 27 6 200706 496513 496513 1898 496513 496513
46 27 7 200707 0 0 0 496513 496513

I want to generate a view which for example for id 45, sum up the val1, val2, val3, val4, val5 for the months of 10,11,and12. and it does the something for id 46 and so on, the length of this table is too big so I need a query to go through different id and look at the different MM (months) to sum up them.

Thank you

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-17 : 11:28:34
[code]
SELECT id,SUM(val1) as val1,SUM(val2) as val2,....SUM(val5) AS val5
FROM Tbl
where [MM] IN (10,11,12)
GROUP BY id
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -