我有這樣的數據在SQL Server日期範圍分組每月
Person StartDate EndDate Value
-----------------------------------------------------------------
38523 2013-01-10 22:00:00.000 2013-01-10 22:59:00.000 0.064
38523 2013-01-10 23:00:00.000 2013-01-10 23:59:00.000 0.065
38523 2013-01-12 00:00:00.000 2013-01-12 00:59:00.000 0.068
38523 2013-01-12 01:00:00.000 2013-01-12 01:59:00.000 0.069
38523 2013-01-12 02:00:00.000 2013-01-12 02:59:00.000 0.069
38523 2013-01-12 03:00:00.000 2013-01-12 03:59:00.000 0.069
38523 2013-01-31 23:00:00.000 2013-01-31 23:59:00.000 0.061
38523 2013-02-01 00:00:00.000 2013-02-01 00:59:00.000 0.064
我需要在分組這樣的方式連續值應該每月被組合在一起。 如果在上一個結束日期和下一個開始日期之間的間隔超過1分鐘,那麼應該將其分成單獨的組。
預期的結果:
Person StartDate EndDate Sum(Value)
----------------------------------------------------------------------
38523 2013-01-10 22:00:00.000 2013-01-10 23:59:00.000 0.129
38523 2013-01-12 00:00:00.000 2013-01-12 03:59:00.000 0.275
38523 2013-01-31 23:00:00.000 2013-01-31 23:59:00.000 0.061 (even though continuity exists, month was end hence separate record)
38523 2013-02-01 00:00:00.000 2013-02-01 00:59:00.000 0.064
任何幫助表示讚賞。
你已經試過了什麼? – MarcinJuraszek 2013-03-20 05:35:45
你能解釋一下嗎?「如果在前一個結束日期和下一個開始日期之間的間隔超過1分鐘,那麼應該將其分成單獨的組。」 – 2013-03-20 06:43:53