| Author |
Topic |
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2009-07-21 : 10:30:51
|
| [code]Date Value2009-01-20 07:00:00.000 02009-01-20 08:00:00.000 3104.042009-01-20 09:00:00.000 3022.312009-01-20 10:00:00.000 2895.842009-01-20 11:00:00.000 3074.242009-01-20 12:00:00.000 4502.022009-01-20 13:00:00.000 3925.09[/code]How to get the sum of values in slots of 2 hours? |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-07-21 : 10:43:13
|
Not sure what you asking... Hope can help...but advise to wait pros with confirmation... |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2009-07-21 : 10:47:29
|
| I want the sum of values from 2009-01-20 07:00:00.000 to 2009-01-20 09:00:00.000 then again from 2009-01-20 09:00:00.000 to 2009-01-20 11:00:00.000 and so on. since the difference is 2 hours between them. |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-07-21 : 10:52:34
|
Does this work?/*Date Value2009-01-20 07:00:00.000 02009-01-20 08:00:00.000 3104.042009-01-20 09:00:00.000 3022.312009-01-20 10:00:00.000 2895.842009-01-20 11:00:00.000 3074.242009-01-20 12:00:00.000 4502.022009-01-20 13:00:00.000 3925.09*/DECLARE @foo TABLE ( [date] DATETIME , [value] MONEY )INSERT @foo ([date], [value]) SELECT '2009-01-20 07:00:00.000', 0UNION SELECT '2009-01-20 08:00:00.000', 3104.04UNION SELECT '2009-01-20 09:00:00.000', 3022.31UNION SELECT '2009-01-20 10:00:00.000', 2895.84UNION SELECT '2009-01-20 11:00:00.000', 3074.24UNION SELECT '2009-01-20 12:00:00.000', 4502.02UNION SELECT '2009-01-20 13:00:00.000', 3925.09-- Test end and start of dayUNION SELECT '2009-01-20 00:00:01.000', 1231.03UNION SELECT '2009-01-20 01:59:00.000', 1235.03UNION SELECT '2009-01-20 22:00:00.000', 122.21UNION SELECT '2009-01-20 23:59:59.000', 0SELECT samp.[strippedDate] , samp.[hourSlot] , SUM(samp.[value])FROM ( SELECT CAST(FLOOR(CAST(f.[date] AS FLOAT)) AS DATETIME) AS [strippedDate] , f.[value] AS [value] , CASE WHEN DATEPART(HOUR, f.[date]) % 2 <> 0 THEN DATEPART(HOUR, f.[date]) - 1 ELSE DATEPART(HOUR, f.[date]) END AS [hourSlot] FROM @foo f ) sampGROUP BY samp.[strippedDate] , samp.[hourSlot] Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2009-07-22 : 03:36:04
|
| No it fails when the same hour falls on multiple days.The hour slot is dynamic.It can even range from 1 to 100 hours. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-22 : 03:55:21
|
quote: Originally posted by ayamas No it fails when the same hour falls on multiple days.The hour slot is dynamic.It can even range from 1 to 100 hours.
what do you mean ? if the hour slot is 100, what is the reference point ?can show with more sample and expected result ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2009-07-22 : 03:58:38
|
| See this sample dataDECLARE @foo TABLE ( [date] DATETIME , [value] MONEY )INSERT @foo ([date], [value]) SELECT '2009-01-20 07:00:00.000', 0UNION SELECT '2009-01-20 08:00:00.000', 3104.04UNION SELECT '2009-01-20 09:00:00.000', 3022.31UNION SELECT '2009-01-20 10:00:00.000', 2895.84UNION SELECT '2009-01-20 11:00:00.000', 3074.24UNION SELECT '2009-01-20 12:00:00.000', 4502.02UNION SELECT '2009-01-20 13:00:00.000', 3925.09Now if reference is 100 hours I need to get the sum of all values from first record till the next 100 hours grouped together.Say if it is 2 hrs the output will be 3104.4 for first slot then3022.31+ 2895.84 for next slot of 2 hrs & so on. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-22 : 04:03:37
|
[code]declare @ref_date datetime, @hour_slot intselect @hour_slot = 3select @ref_date = min([date])from @fooselect datediff(hour, @ref_date, [date]) / @hour_slot, sum([value])from @foogroup by datediff(hour, @ref_date, [date]) / @hour_slot[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2009-07-22 : 04:51:52
|
| Thanks khtan it worked.I was little close but could not finish it.I was using this oneselect datepart(hh,date)/hourslot,sum(value) from @foogroup by datepart(hh,date))/hourslotorder by DATEPART(HH,date).Thanks once again man. |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-07-22 : 04:52:24
|
quote: Originally posted by ayamas No it fails when the same hour falls on multiple days.The hour slot is dynamic.It can even range from 1 to 100 hours.
Don't you think it would have been a good idea to say this in the first place? Then I wouldn't have wasted my time.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2009-07-22 : 04:56:57
|
quote: Originally posted by Transact Charlie
quote: Originally posted by ayamas No it fails when the same hour falls on multiple days.The hour slot is dynamic.It can even range from 1 to 100 hours.
Don't you think it would have been a good idea to say this in the first place? Then I wouldn't have wasted my time.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Sorry charlie.I am really sorry from my heart.I will be more specific from next time.Thanks for taking troubles. |
 |
|
|
|