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 |
|
CLages
Posting Yak Master
116 Posts |
Posted - 2005-04-28 : 14:15:15
|
| Hi , its me again I have a table like thisDate(DD/MM/YYYY) Value etc01-04-2005 100,00 ...01-04-2005 200,00 ...02-04-2005 230,00 ... 02-04-2005 130,00 ... 03-04-2005 200,00 ... 04-04-2005 120,00 ... 04-04-2005 220,00 ... 05-04-2005 300,00 ... if the user type a range of data 02/04/2005 to 05-04-2005if i do select * from ( select 'Ant' as Data, sum(dup_valor) as Valor from duplicat where dup_data < 02/04/2005 )union allselect * from ( select cast(dup_data as char(12)) as Data, sum(dup_valor) as Valor from duplicat where dup_data Between 02/04/2005 and 05/04/2005 group by dup_data )order by dup_dataI get Data Valor Ant 30002-04-05 36003-04-05 20004-04-05 34005-04-05 300works fine. but i would like get something like thisData Valor Accumulated Ant 300 300 02-04-05 360 660 03-04-05 200 86004-04-05 340 1200 05-04-05 300 1500How can i Implement the column Accumulated?tksCarlos Lages |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-04-28 : 17:27:20
|
Run this in a QA window to test:--set up the table (if you did this I wouldn't need to guess the datatypes)set nocount onset dateformat dmydeclare @duplicat table (dup_data smalldatetime, dup_valor int)insert @duplicatselect '01-04-2005', 100 union allselect '01-04-2005', 200 union allselect '02-04-2005', 230 union allselect '02-04-2005', 130 union allselect '03-04-2005', 200 union allselect '04-04-2005', 120 union allselect '04-04-2005', 220 union allselect '05-04-2005', 300--user inputdeclare @dtLow smalldatetime ,@dtHigh smalldatetimeselect @dtLow = convert(smalldatetime,'02/04/2005') ,@dtHigh = convert(smalldatetime,'05/04/2005')--sql statmentselect Data = case when dat = @dtLow -1 then 'Ant' else convert(varchar,Dat,105) end ,valor = sum(valor) ,accumulated = (select sum(dup_valor) from @duplicat where dup_data <= a.dat)From ( select Dat = case when dup_data < @dtLow then @dtLow -1 when dup_data between @dtLow and @dtHigh then dup_data else null end ,valor = dup_valor from @Duplicat Where dup_data <= @dtHigh ) agroup by datorder by dat Be One with the OptimizerTG |
 |
|
|
CLages
Posting Yak Master
116 Posts |
Posted - 2005-04-28 : 20:29:33
|
| Tg, Thanksfantastic your solutionTks a lotYou get a fanC. Lages |
 |
|
|
CLages
Posting Yak Master
116 Posts |
Posted - 2005-04-28 : 20:45:54
|
| Tg only one question I did not Understand the statement you wrotevalor = sum(valor)where did you declared the variable "Valor" ?tksagain C. Lages |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-04-28 : 21:05:53
|
| You're welcome!valor = sum(valor) is the same as:sum(valor) as Valorvalor is not a variable, it's a column aliasBe One with the OptimizerTG |
 |
|
|
mpetanovitch
Yak Posting Veteran
52 Posts |
Posted - 2005-04-29 : 13:17:25
|
This one is simplier; however it doesn't completely satisfy your requirements.CREATE TABLE #temp (yak varchar(20), value int)INSERT INTO #tempVALUES('1/1/2005', 50)INSERT INTO #tempVALUES('1/2/2005', 50)INSERT INTO #tempVALUES('1/3/2005', 100)INSERT INTO #tempVALUES('1/4/2005', 50)INSERT INTO #tempVALUES('1/5/2005', 50)INSERT INTO #tempVALUES('1/6/2005', 50)INSERT INTO #tempVALUES('1/7/2005', 200)DECLARE @startdate datetime, @enddate datetimeSET @startdate = '1/4/2005'SET @enddate = '1/6/2005'SELECT t.yak, t.value, SUM(t1.value) AS running_totalFROM #temp tLEFT OUTER JOIN #temp t1 ON t.yak >= t1.yak WHERE t.yak BETWEEN @startdate AND @enddateGROUP BY t.yak, t.valueORDER BY 1RESULT SET:yak value running_total1/4/2005 50 250--<--running total - value = "Ant"1/5/2005 50 3001/6/2005 50 350Here would be the complete result set:yak value running_total1/1/2005 50 501/2/2005 50 1001/3/2005 100 2001/4/2005 50 2501/5/2005 50 3001/6/2005 50 3501/7/2005 200 550Notice that the running total is still calculated even though the where clause removes them from view in the first result set.The requrement does not satisfied is the "ant" line which totals all values before the startdate. Mike Petanovitch |
 |
|
|
|
|
|
|
|