| Author |
Topic |
|
Ken Blum
Constraint Violating Yak Guru
383 Posts |
Posted - 2005-06-16 : 07:39:54
|
| I have the following data in a table...DateTime Column Numeric Column 6/14/05 08:00:00 56/14/05 09:00:00 86/14/05 10:00:00 126/14/05 11:00:00 246/14/05 12:00:00 06/14/05 13:00:00 106/14/05 14:00:00 26/14/05 15:00:00 166/14/05 17:00:00 226/14/05 18:00:00 33How can I get the sum of the variances? (24-5)+(10-0)+(33-2) = 60TIA! |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-06-16 : 07:50:52
|
| How did you define variances?MadhivananFailing to plan is Planning to fail |
 |
|
|
Ken Blum
Constraint Violating Yak Guru
383 Posts |
Posted - 2005-06-16 : 09:42:31
|
| This data comes from a PLC which is a counter which can be reset at any point by a user. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-06-16 : 09:53:00
|
| I asked you on what basis you calculated?MadhivananFailing to plan is Planning to fail |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-06-16 : 09:57:54
|
quote: Originally posted by madhivanan I asked you on what basis you calculated?MadhivananFailing to plan is Planning to fail
more specifically: why do you show (24-5) and (10-0) what is the connection between each of the pair?Corey Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now." |
 |
|
|
Ken Blum
Constraint Violating Yak Guru
383 Posts |
Posted - 2005-06-16 : 09:59:55
|
| I want to calculate the sum of the variances for June 14th. It starts out with 5 and goes up to 24, so that's a variance of 19. Then it starts out at 0 and goes up to 10, so that's a variance of 10. Then it starts out at 2 and goes up to 33, so that's a variance of 31. The total of the variances of 19+10+31 = 60. So the sum of the variances for June 14th is 60. |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-06-16 : 10:21:05
|
so the you are interested in the difference of a climb.if a number is less than the one before, it restarts the counter (so to speak)Create Table #myTable (rDate datetime, rVal int)Insert Into #myTableSelect '6/14/05 08:00:00', 5 Union All Select '6/14/05 09:00:00', 8 Union All Select '6/14/05 10:00:00', 12 Union All Select '6/14/05 11:00:00', 24 Union All Select '6/14/05 12:00:00', 0 Union All Select '6/14/05 13:00:00', 10 Union All Select '6/14/05 14:00:00', 2 Union All Select '6/14/05 15:00:00', 16 Union All Select '6/14/05 17:00:00', 22 Union All Select '6/14/05 18:00:00', 33Select ttlVariance = sum(vStop - vStart)From ( Select rDate, vStart = rVal, vStop = (Select top 1 rVal from (Select rDate, rVal From #myTable A Where isnull((select top 1 rVal From #myTable Where rDate > A.rDate Order By rDate),rVal) <= rVal) Y where rDate > Z.rDate order by rDate) From ( Select rDate, rVal From #myTable A Where isnull((select top 1 rVal From #myTable Where rDate < A.rDate Order By rDate desc),rVal) >= rVal ) Z ) A Drop Table #myTable Corey Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now." |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-06-16 : 10:27:14
|
| Seventhnight,You proved that you are GeniusMadhivananFailing to plan is Planning to fail |
 |
|
|
Ken Blum
Constraint Violating Yak Guru
383 Posts |
Posted - 2005-06-16 : 10:34:02
|
| SeventhNight: Totally cool. Thanks. So, just what happened on the Seventh Night anyway? |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-06-16 : 10:52:38
|
how bout this:declare @t table (dt datetime, num int)insert into @tselect '6/14/05 08:00:00', 5 union allselect '6/14/05 09:00:00', 8 union allselect '6/14/05 10:00:00', 12 union allselect '6/14/05 11:00:00', 24 union allselect '6/14/05 12:00:00', 0 union allselect '6/14/05 13:00:00', 10 union allselect '6/14/05 14:00:00', 2 union allselect '6/14/05 15:00:00', 16 union allselect '6/14/05 17:00:00', 22 union allselect '6/14/05 18:00:00', 33select *from @tselect sum(num)from ((select numfrom @t t1 where num > isnull((select top 1 num from @t where dt > t1.dt), 0))union all(select num*-1from @t t1 where num <= isnull((select top 1 num from @t where dt <= t1.dt), 0))) t could't help myself Go with the flow & have fun! Else fight the flow |
 |
|
|
Ken Blum
Constraint Violating Yak Guru
383 Posts |
Posted - 2005-06-16 : 10:58:01
|
| Ooooh, I like that even better! Even my simple mind can understand this one!Thanks. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-06-16 : 10:59:46
|
| Mladen, tell me Is sql programming your hobby?MadhivananFailing to plan is Planning to fail |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-06-16 : 11:01:33
|
yes it is... plus it looks weel on my resumé...it's turning into an addiction nowadays... Go with the flow & have fun! Else fight the flow |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-06-16 : 11:11:27
|
pbbbbtSelect ttlVariance = sum(Variance)From ( Select variance = rVal - isnull((Select top 1 rVal From #myTable where rDate < A.rDate Order BY rDate desc),rVal) From #myTable A ) ZWhere variance > 0 TAG!!! You're It!Corey Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now." |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-06-16 : 11:19:10
|
ROTFLOL!!     i guess that's as far as it goes...Go with the flow & have fun! Else fight the flow |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-06-16 : 11:29:57
|
quote: could't help myself 
Corey Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now." |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-06-16 : 11:37:49
|
emmm something weird...execution plans give me that your query is "costlier" than mine... weird...Go with the flow & have fun! Else fight the flow |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-06-16 : 11:50:00
|
using table variable for both?? what if you indexed the temp table?also, don't you need to specify order bys on your 'select top 1..' subqueries??Corey Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now." |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-06-16 : 11:57:54
|
quote: Originally posted by spirit1 emmm something weird...execution plans give me that your query is "costlier" than mine... weird...Go with the flow & have fun! Else fight the flow 
This came out about 4x cheaper than your [pppbbbbt] again  Create Table #myTable (rDate datetime, rVal int)Insert Into #myTableSelect '6/14/05 08:00:00', 5 Union All Select '6/14/05 09:00:00', 8 Union All Select '6/14/05 10:00:00', 12 Union All Select '6/14/05 11:00:00', 24 Union All Select '6/14/05 12:00:00', 0 Union All Select '6/14/05 13:00:00', 10 Union All Select '6/14/05 14:00:00', 2 Union All Select '6/14/05 15:00:00', 16 Union All Select '6/14/05 17:00:00', 22 Union All Select '6/14/05 18:00:00', 33Declare @ttlVariance int, @lastVal intSelect @ttlVariance = isnull(@ttlVariance,0) + case when rVal > isnull(@lastVal,rVal) then rVal - @lastVal else 0 end, @lastVal = rValFrom #myTableSelect @ttlVarianceDrop Table #myTable Corey Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now." |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-06-16 : 11:58:52
|
this is what i run.i forgot the order by before...i get 10% batch cost for insert, 90% for yours and 0% for mine... do you get the same?declare @t table (dt datetime, num int)insert into @tselect '6/14/05 08:00:00', 5 union allselect '6/14/05 09:00:00', 8 union allselect '6/14/05 10:00:00', 12 union allselect '6/14/05 11:00:00', 24 union allselect '6/14/05 12:00:00', 0 union allselect '6/14/05 13:00:00', 10 union allselect '6/14/05 14:00:00', 2 union allselect '6/14/05 15:00:00', 16 union allselect '6/14/05 17:00:00', 22 union allselect '6/14/05 18:00:00', 33Select ttlVariance = sum(Variance)From ( Select variance = num - isnull((Select top 1 num From @t where dt < A.dt Order BY dt desc), num) From @t A ) ZWhere variance > 0select sum(num) as varianceSumfrom ((select numfrom @t t1 where num > isnull((select top 1 num from @t where dt > t1.dt order by dt asc), 0))union all(select num*-1from @t t1 where num <= isnull((select top 1 num from @t where dt <= t1.dt order by dt asc), 0))) t Go with the flow & have fun! Else fight the flow |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-06-16 : 12:00:16
|
what a hell is a [pppbbbbt]????Go with the flow & have fun! Else fight the flow |
 |
|
|
Next Page
|