-1

I am having a warehouse Fact Table containing raw data (Even Duplicate Data) received from vendor through a data feed. I need to prepare the chunk of 15 Min Interval data. How can I best SQL Server query to do this. E.g.Sample Data

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 
backtrack
  • 7,996
  • 5
  • 52
  • 99
Veneet
  • 41
  • 9

2 Answers2

0

Batching in fixed batches for each quarter of the hour, by constructing a new column that specifies the start of each batch:

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]

Result for your example:

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
CyberDude
  • 8,541
  • 5
  • 29
  • 47
0

This will round down to nearest 15 minutes

SELECT dateadd(minute, datediff(minute, 0, Date)/15*15, 0) 
FROM yourtable
t-clausen.dk
  • 43,517
  • 12
  • 59
  • 92