| Author |
Topic |
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-08-04 : 21:30:40
|
[code]DECLARE @tempfun TABLE( col1 int, col2 DATETIME)INSERT INTO @tempfunSELECT 1, '20070101' UNION ALLSELECT 2, '20070102' UNION ALLSELECT 3, '20070201' UNION ALLSELECT 4, '20070202' UNION ALLSELECT 5, '20070301' UNION ALLSELECT 6, '20070302' UNION ALLSELECT 7, '20070401' UNION ALLSELECT 8, '20070402' UNION ALLSELECT 9, '20070501' UNION ALLSELECT 10, '20070502'SELECT SUM(col1) as TotalSUMFROM @tempfunWHERE col2<='20070502'[/code]This will getTotalSUM55How i can getTotalSUM | PrevousTotalSUM55 | 45Edit. wrong calculation.. Hope can help...but advise to wait pros with confirmation... |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-08-04 : 21:46:51
|
[code]SELECT SUM(a.col1)TotalSUM, (SELECT SUM(col1) FROM (SELECT row_number() over (order by col1, col2)col0, col1 FROM @tempfun where col2 <='20070502' )a WHERE col0<=(SELECT count(*) FROM @tempfun where col2 <='20070502')-1)PreviousTotalSUMFROM @tempfun aWHERE a.col2 <='20070502'[/code]solved but slow... Hope can help...but advise to wait pros with confirmation... |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-08-04 : 22:27:07
|
anyone with better query will be helpful...thx first Hope can help...but advise to wait pros with confirmation... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-08-04 : 22:38:56
|
[code]SELECT TotalSUM = SUM(col1), PreviousTotalSUM = SUM(CASE WHEN row_no > 1 THEN col1 ELSE 0 END)FROM( SELECT col1, col2, row_no = row_number() OVER (ORDER BY col2 DESC) FROM @tempfun WHERE col2 <= '20070502') t[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-08-04 : 22:54:45
|
thx sifu...i seems to be never remember got "DESC" this key word  Hope can help...but advise to wait pros with confirmation... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-08-04 : 22:58:20
|
 KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-08-05 : 00:53:08
|
Hi try this once,DECLARE @tempfun TABLE( col1 int, col2 DATETIME)INSERT INTO @tempfunSELECT 1, '20070101' UNION ALLSELECT 2, '20070102' UNION ALLSELECT 3, '20070201' UNION ALLSELECT 4, '20070202' UNION ALLSELECT 5, '20070301' UNION ALLSELECT 6, '20070302' UNION ALLSELECT 7, '20070401' UNION ALLSELECT 8, '20070402' UNION ALLSELECT 9, '20070501' UNION ALLSELECT 10, '20070502'declare @maxvalue datetimeselect @maxvalue = max(col2) from @tempfunselect sum(col1) from @tempfun where col2< @maxvalue |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-08-05 : 03:11:06
|
Can I play too?-- Peso 1SELECT SUM(Col1) AS Total, SUM(COALESCE(NULLIF(Col1, theMAX), 0)) AS PreviousFROM ( SELECT Col1, MAX(Col1) OVER () AS theMax FROM @TempFun ) AS d-- Peso 2SELECT SUM(Col1) AS Total, SUM(CASE Col2 WHEN theMAX THEN 0 ELSE Col1 END) AS PreviousFROM ( SELECT Col1, Col2, MAX(Col2) OVER () AS theMax FROM @TempFun ) AS d N 56°04'39.26"E 12°55'05.63" |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-08-05 : 22:35:15
|
hehehe...thx alot for so many different method...today dunno why im so dam sleepy!!!so hard to focus what im doing T.T Hope can help...but advise to wait pros with confirmation... |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-08-05 : 22:41:05
|
btw Nageswar9, you din't actually solve the question... Hope can help...but advise to wait pros with confirmation... |
 |
|
|
|