-1

我有一个仓库事实表,其中包含通过数据馈送从供应商处收到的原始数据(甚至是重复数据)。我需要准备 15 分钟间隔数据块。我怎样才能最好的 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 
4

2 回答 2

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
于 2015-03-05T08:19:54.157 回答
0

这将四舍五入到最接近的 15 分钟

SELECT dateadd(minute, datediff(minute, 0, Date)/15*15, 0) 
FROM yourtable
于 2015-03-05T08:34:25.060 回答