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)
 Want to add row data

Author  Topic 

anuragsri12
Starting Member

1 Post

Posted - 2011-11-04 : 01:27:07
Hi

I have a problem, want to add row data and GROUP the data by using SrNo and Months.

SrNo Months Value
1 Jan-2010 2
1 Jan-2010 5
2 Feb-2010 1
2 Feb-2010 6
2 Feb-2010 2
3 Mar-2010 7
3 Mar-2010 4

The result set should be like this:

SrNo Months Value
1 Jan-2010 7 (Sum of Jan-2010 value)
2 Feb-2010 16 (Jan-2010 + Feb-2010)
3 Mar-2010 27 (Jan-2010 + Feb-2010 + Mar-2010)

Help on urgent basis.
Thanks in advance
Regards
Anurag

Anurag

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-04 : 04:40:52
[code]
SELECT SrNo,Months,TotVal
FROM (SELECT DISTINCT SrNo,Months FROM table)t
CROSS APPLY(SELECT SUM(Value) AS TotVal
FROM table
WHERE CAST(REPLACE(Months,'-',' ') AS datetime) <=CAST(REPLACE(t.Months,'-',' ') AS datetime)
)t1
[/code]

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

Go to Top of Page
   

- Advertisement -