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.
Author |
Topic |
jitendra.sawant
Starting Member
2 Posts |
Posted - 2012-05-07 : 03:29:27
|
Hi,i am using a queryselect year(a.bod_dt) as year, month(a.bod_dt)as month, Sum(Cast(a.THV AS MONEY)) as ThvSum from Main_Calc_NEW as a group by year(a.bod_dt), month(a.bod_dt) order by year(a.bod_dt) , month(a.bod_dt) and getting a resultyear month ThvSum2010 12 7900785.002011 1 220043056.002011 2 273445394.002011 3 286116658.002011 4 293334502.002011 5 340162084.002011 6 384224662.002011 7 643044951.002011 8 845888118.002011 9 1241187393.002011 10 2025064350.00I want output to be year month ThvSum Balance2010 12 7900785 79007852011 1 220043056 2279438412011 2 273445394 5013892352011 3 286116658 7875058932011 4 293334502 10808403952011 5 340162084 14210024792011 6 384224662 1805227141where 'Balance' values will be sum of this rows ThvSum + previous rows ThvSum..Thanks in advance.. |
|
Sachin.Nand
2937 Posts |
Posted - 2012-05-07 : 03:30:56
|
Just Google "running total in sql server".After Monday and Tuesday even the calendar says W T F .... |
 |
|
jitendra.sawant
Starting Member
2 Posts |
Posted - 2012-05-07 : 05:08:06
|
Got the solution I did it like thisDECLARE @DateStart date= (select min(convert(datetime,bod_dt,112)) from main_calc_new);WITH CTE AS (SELECT ID = ROW_NUMBER() OVER(ORDER BY year(bod_dt) , month(bod_dt)), year(bod_dt) as year, month(bod_dt)as month,Sum(Cast(THV AS MONEY)) as THV FROM main_calc_new WHERE convert(datetime,bod_dt,112) >= @DateStart group by year(bod_dt) , month(bod_dt))SELECT [year], [month], [THV], Balance = (SELECT SUM(THV) FROM CTE WHERE ID <= A.ID)FROM CTE AS AORDER BY ID |
 |
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2012-05-09 : 20:36:21
|
[code]SELECT * FROM YOURTABLE X CROSS APPLY ( SELECT SUM(y.THSUM)AS [RuningTotal] FROM YOURTABLE Y WHERE Y.THSUM <= X.THSUM)TAB[/code] |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-10 : 00:14:27
|
quote: Originally posted by jitendra.sawant Hi,i am using a queryselect year(a.bod_dt) as year, month(a.bod_dt)as month, Sum(Cast(a.THV AS MONEY)) as ThvSum from Main_Calc_NEW as a group by year(a.bod_dt), month(a.bod_dt) order by year(a.bod_dt) , month(a.bod_dt) and getting a resultyear month ThvSum2010 12 7900785.002011 1 220043056.002011 2 273445394.002011 3 286116658.002011 4 293334502.002011 5 340162084.002011 6 384224662.002011 7 643044951.002011 8 845888118.002011 9 1241187393.002011 10 2025064350.00I want output to be year month ThvSum Balance2010 12 7900785 79007852011 1 220043056 2279438412011 2 273445394 5013892352011 3 286116658 7875058932011 4 293334502 10808403952011 5 340162084 14210024792011 6 384224662 1805227141where 'Balance' values will be sum of this rows ThvSum + previous rows ThvSum..Thanks in advance..
see scenario 1 http://visakhm.blogspot.com/2010/01/multipurpose-apply-operator.html------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|