我遇到的问题是,我有一张包含数百万小时记录的表,需要创建一个查询来压缩变更数据。
示例数据 -
Start DateTime End DateTime Value
2015-09-13 6:00 2015-09-13 7:00 45
2015-09-13 7:00 2015-09-13 8:00 45
2015-09-13 8:00 2015-09-13 9:00 45
2015-09-13 9:00 2015-09-13 10:00 45
2015-09-13 10:00 2015-09-13 11:00 45
2015-09-13 11:00 2015-09-13 12:00 45
2015-09-13 12:00 2015-09-13 13:00 25
2015-09-13 13:00 2015-09-13 14:00 25
2015-09-13 14:00 2015-09-13 15:00 25
2015-09-13 15:00 2015-09-13 16:00 45
2015-09-13 16:00 2015-09-13 17:00 45
2015-09-13 17:00 2015-09-13 18:00 45
2015-09-13 18:00 2015-09-13 19:00 45
预期结果 -
Start DateTime End DateTime Value
2015-09-13 6:00 2015-09-13 12:00 45
2015-09-13 12:00 2015-09-13 15:00 25
2015-09-13 15:00 2015-09-13 19:00 45
非常感谢任何帮助。
谢谢和问候
答案 0 :(得分:0)
排名功能可能会对您有所帮助。
假设SampleData
- 是包含这些数据的表,
这可能很慢,
WITH HourlyData AS (
SELECT RANK() OVER (ORDER BY StartDate) AS RankId
, StartDate, EndDate, Value
FROM SampleDate),
DateHourChanged AS (
SELECT DENSE_RANK () OVER (ORDER BY (CASE WHEN A.Value <> B.value THEN 1 ELSE A.RankId END)) AS ID
, A.StartDate, A.EndDate, A.Value
FROM HourlyData AS A
LEFT JOIN HourlyData AS B
ON A.RankId -1 = B.RankId
)
SELECT StartDate, EndDate, Value
FROM DateHourChanged
WHERE Id = 1
ORDER BY 1
我选择了上一小时和当前小时,当前小时的值与前一小时不同,然后记录当前小时。