所以我使用this帖子作为参考,但是我想根据15分钟的时间段计算所有行。
这是我到目前为止所做的:
SELECT DateAdd(minute, DateDiff(minute, 0, [datetime]), 0) as Timestamp,
Count(*) as Tasks
FROM [table]
GROUP BY DateAdd(minute, DateDiff(minute, 0, [datetime]), 0)
ORDER BY Timestamp
这非常适合每分钟获取行数,但我需要15分钟...... 所以我改变了:
DateAdd(minute, DateDiff(minute, 0, [datetime]), 0)
到
DateAdd(minute, DateDiff(minute, 0, [datetime]), 15)
感谢任何帮助!
答案 0 :(得分:3)
要15分钟,除以15(然后再乘以):
SELECT DateAdd(minute, 15*(DateDiff(minute, 0, [datetime]) / 15), 0
) as Timestamp,
Count(*) as Tasks
FROM [table]
GROUP BY (DateDiff(minute, 0, [datetime]) / 15)
ORDER BY Timestamp;
SQL Server执行整数除法。如果您想明确表达自己的意图,请使用FLOOR()
。
答案 1 :(得分:0)
SELECT ROUND(DATEDIFF(SECOND,{d '1970-01-01'},[datetime])/(15 * 60),0) as Timestamp,
Count(*) as Tasks
FROM [table]
GROUP BY ROUND(DATEDIFF(SECOND,{d '1970-01-01'},[datetime])/(15 * 60),0)
ORDER BY Timestamp
答案 2 :(得分:0)
如果整数除法对您造成问题,可以选择以下选项。它将日期时间转换为浮点数,然后使用floor()。
SELECT convert(varchar,cast(round(floor(cast([datetime] as float(53))*24*4)/(24*4),5) as smalldatetime),108) as Timestamp,
Count(*) as Tasks
FROM [table]
GROUP BY convert(varchar,cast(round(floor(cast([datetime] as float(53))*24*4)/(24*4),5) as smalldatetime),108)
ORDER BY Timestamp
我通常会将(24 * 4)更改为96(一天中15分钟的间隔数),但我想我会离开它,以便人们可以看到如何在其他时间段内调整它。