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 2005 Forums
 Transact-SQL (2005)
 summing the cost column and group by Year

Author  Topic 

rum23
Yak Posting Veteran

77 Posts

Posted - 2009-06-11 : 08:13:51
ID2 Year Cost TimeStamp ID1
25 2006 749411.76 0x0000000000008CC0 12
25 2007 1001960.78 0x0000000000008CC1 12
25 2008 1111705.88 0x0000000000008CC2 12
25 2009 1001960.78 0x0000000000008CC3 12
25 2010 1001960.78 0x0000000000008CC4 12
25 2011 1002598.43 0x0000000000008CC5 12
25 2012 1008661.42 0x0000000000008CC6 12
25 2013 228740.17 0x0000000000008CC7 12
26 2006 12678.76 0x0000000000008CC9 12
26 2007 1001960.78 0x0000000000008CCA 12
26 2008 1004705.88 0x0000000000008CCB 12
26 2009 1001960.78 0x0000000000008CCC 12
26 2010 1001960.78 0x0000000000008CCD 12
26 2011 1002598.43 0x0000000000008CCE 12
26 2012 1008661.42 0x0000000000008CCF 12
26 2013 228740.17 0x0000000000008CD0 12

What is the SQL to get the following output? I want to sum the Cost by Year. Eg. For Year 2008, the cost should be 1004705.88 + 1111705.88

The output should be like this.

ID1 Year Cost
12 2006 749411.76 + 12678.76
12 2007 XXXXX
12 2008 1004705.88 + 1111705.88 and so forth.

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-06-11 : 08:27:43
Simply this...... ?

SELECT
[Id1]
, [Year]
, SUM([cost])
FROM
<theTable>
GROUP BY
[Id1]
, [Year]
ORDER BY
[Year] ASC



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -