Group by Time Interval

前端 未结 2 1082
醉梦人生
醉梦人生 2021-01-28 02:46

I need to Group my Table into 15 minutes Intervals. I can do that with:

select dateadd(minute, datediff(minute, 0, ts) / 15 * 15, 0), sum (goodpieces) 
from Stat         


        
2条回答
  •  北海茫月
    2021-01-28 03:43

    Create a table with every possible timestamp in 15 minute increments, and do a LEFT JOIN from it to your query above.

    SELECT * FROM timestamps LEFT JOIN (SELECT dateadd......) ON timestamps.timestamp = StationCount.ts
    

    If you know your chart always covers a 24 hour period, you only need to create a table with the numbers 0-95, then for each entry add it to the start time of your chart.

    SELECT *
      FROM (SELECT dateadd(minute, , number*15) timestamp FROM numbers) timestamps LEFT JOIN
           (SELECT dateadd......) ON timestamps.timestamp = StationCount.ts
    

提交回复
热议问题