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 |
|
pmotewar
Yak Posting Veteran
62 Posts |
Posted - 2009-08-17 : 10:00:52
|
Hi all,following are my table data Date Units27/10/2008 3450.995903/11/2008 4.375124/11/2008 7.093901/12/2008 5.323208/12/2008 9.174115/12/2008 7.317422/12/2008 5.65729/12/2008 5.096705/01/2009 5.553912/01/2009 4.932213/01/2009 980.565219/01/2009 1.339419/01/2009 4.788227/01/2009 4.793527/01/2009 1.337202/02/2009 1.394402/02/2009 5.012309/02/2009 5.474109/02/2009 1.518516/02/2009 1.206216/02/2009 4.3616 and i want output like thisDate Units Balance Units27/10/2008 3450.9959 3450.995903/11/2008 4.3751 3455.37124/11/2008 7.0939 3462.464901/12/2008 5.3232 3467.788108/12/2008 9.1741 3476.962215/12/2008 7.3174 3484.279622/12/2008 5.657 3489.936629/12/2008 5.0967 3495.033305/01/2009 5.5539 3500.587212/01/2009 4.9322 3505.519413/01/2009 980.5652 4486.084619/01/2009 1.3394 4487.42419/01/2009 4.7882 4492.212227/01/2009 4.7935 4497.005727/01/2009 1.3372 4498.342902/02/2009 1.3944 4499.737302/02/2009 5.0123 4504.749609/02/2009 5.4741 4510.223709/02/2009 1.5185 4511.742216/02/2009 1.2062 4512.948416/02/2009 4.3616 4517.31balance unit = sum(units) [all previous units] + current units please help me Pankaj |
|
|
Sachin.Nand
2937 Posts |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-08-17 : 11:18:02
|
[code]declare @sample table( [Date] datetime, [Units] decimal(10,4))set dateformat dmyinsert into @sampleselect '27/10/2008', 3450.9959 union allselect '03/11/2008', 4.3751 union allselect '24/11/2008', 7.0939 union allselect '01/12/2008', 5.3232 union allselect '08/12/2008', 9.1741 union allselect '15/12/2008', 7.3174 union allselect '22/12/2008', 5.6570 union allselect '29/12/2008', 5.0967 union allselect '05/01/2009', 5.5539 union allselect '12/01/2009', 4.9322 union allselect '13/01/2009', 980.5652 union allselect '19/01/2009', 1.3394 union allselect '19/01/2009', 4.7882 union allselect '27/01/2009', 4.7935 union allselect '27/01/2009', 1.3372 union allselect '02/02/2009', 1.3944 union allselect '02/02/2009', 5.0123 union allselect '09/02/2009', 5.4741 union allselect '09/02/2009', 1.5185 union allselect '16/02/2009', 1.2062 union allselect '16/02/2009', 4.3616select *, [Balance Units] = (select sum(Units) from @sample x where x.[Date] <= s.[Date])from @sample sorder by s.[Date]select *from @sample s cross apply ( select [Balance Units] = sum(Units) from @sample x where x.[Date] <= s.[Date] ) border by s.[Date]/*Date Units Balance Units ------------------------------------------------------ ------------ ---------------------------------------- 2008-10-27 00:00:00.000 3450.9959 3450.99592008-11-03 00:00:00.000 4.3751 3455.37102008-11-24 00:00:00.000 7.0939 3462.46492008-12-01 00:00:00.000 5.3232 3467.78812008-12-08 00:00:00.000 9.1741 3476.96222008-12-15 00:00:00.000 7.3174 3484.27962008-12-22 00:00:00.000 5.6570 3489.93662008-12-29 00:00:00.000 5.0967 3495.03332009-01-05 00:00:00.000 5.5539 3500.58722009-01-12 00:00:00.000 4.9322 3505.51942009-01-13 00:00:00.000 980.5652 4486.08462009-01-19 00:00:00.000 1.3394 4492.21222009-01-19 00:00:00.000 4.7882 4492.21222009-01-27 00:00:00.000 4.7935 4498.34292009-01-27 00:00:00.000 1.3372 4498.34292009-02-02 00:00:00.000 1.3944 4504.74962009-02-02 00:00:00.000 5.0123 4504.74962009-02-09 00:00:00.000 5.4741 4511.74222009-02-09 00:00:00.000 1.5185 4511.74222009-02-16 00:00:00.000 1.2062 4517.31002009-02-16 00:00:00.000 4.3616 4517.3100(21 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-08-18 : 05:31:29
|
| Hi, try this alsothis is another wayselect identity(int,1,1) as id ,* into #temp from @tabselect amt,date,( select sum(amt) from #temp where id <= t.id )as total from #temp t |
 |
|
|
|
|
|
|
|