我有一个仓库Fact Table,其中包含通过数据Feed从供应商处收到的原始数据(甚至是重复数据)。我需要准备15 Min Interval数据块。我怎样才能最好的SQL Server查询来做到这一点。例如。样本数据
ID key Date Value
1 1 2013-10-08 00:00:00.000 10
2 1 2013-10-08 00:23:00.000 15
3 1 2013-10-08 01:00:00.000 20
4 1 2013-10-08 01:15:00.000 25
5 1 2013-10-08 01:30:00.000 30
6 1 2013-10-08 01:35:00.000 30
7 1 2013-10-08 01:50:00.000 30
8 1 2013-10-08 01:55:00.000 30
答案 0 :(得分:0)
通过构建一个指定每批次开始的新列,以每小时的固定批次进行批处理:
SELECT
*,
CASE
WHEN (DATEPART(minute, [Date]) >= 0 AND DATEPART(minute, [Date]) < 15) THEN DATETIMEFROMPARTS (DATEPART(year, [Date]), DATEPART(month, [Date]), DATEPART(day, [Date]), DATEPART(hour, [Date]), 0, 0, 0)
WHEN (DATEPART(minute, [Date]) >= 15 AND DATEPART(minute, [Date]) < 30) THEN DATETIMEFROMPARTS (DATEPART(year, [Date]), DATEPART(month, [Date]), DATEPART(day, [Date]), DATEPART(hour, [Date]), 15, 0, 0)
WHEN (DATEPART(minute, [Date]) >= 30 AND DATEPART(minute, [Date]) < 45) THEN DATETIMEFROMPARTS (DATEPART(year, [Date]), DATEPART(month, [Date]), DATEPART(day, [Date]), DATEPART(hour, [Date]), 30, 0, 0)
ELSE DATETIMEFROMPARTS (DATEPART(year, [Date]), DATEPART(month, [Date]), DATEPART(day, [Date]), DATEPART(hour, [Date]), 45, 0, 0)
END AS BatchStart
FROM
Fact
ORDER BY
[Date]
您的示例的结果:
ID key Date Value BatchStart
1 1 2013-10-08 00:00:00.000 10 2013-10-08 00:00:00.000
2 1 2013-10-08 00:23:00.000 15 2013-10-08 00:15:00.000
3 1 2013-10-08 01:00:00.000 20 2013-10-08 01:00:00.000
4 1 2013-10-08 01:15:00.000 25 2013-10-08 01:15:00.000
5 1 2013-10-08 01:30:00.000 30 2013-10-08 01:30:00.000
6 1 2013-10-08 01:35:00.000 30 2013-10-08 01:30:00.000
7 1 2013-10-08 01:50:00.000 30 2013-10-08 01:45:00.000
8 1 2013-10-08 01:55:00.000 30 2013-10-08 01:45:00.000
答案 1 :(得分:0)
这将向下舍入到最近的15分钟
SELECT dateadd(minute, datediff(minute, 0, Date)/15*15, 0)
FROM yourtable