我有一个典型的日志表,其中包含许多详细信息以及创建记录的日期时间。
我试图分析每个时间段(每30分钟)发生一次事件的次数。我需要能够分析超过一天的时间。基本上,我想要的输出类似于:
Period | Total
Day 1 00:00 - 00:30 | 23
Day 1 00:30 - 01:00 | 0
Day 1 01:00 - 01:30 | 534
...
Day 2 23:00 - 23:30 | 23
我对日期栏布局非常灵活 - 我只需要"可读"!
这是我的尝试,但效果不佳
declare @startdatetime datetime = '2016-02-03 00:00:00'
declare @enddatetime datetime = '2016-02-19 23:59:59'
declare @apiserviceid int = 21
select DATEPART(MINUTE, usr.STARTDATETIME) % 30, COUNT(*)
from TABLE 1 usr
where usr.APIREQUESTID = @apiserviceid
and usr.STARTDATETIME >= @startdatetime and usr.STARTDATETIME <= @enddatetime
group by
(DATEPART(MINUTE, usr.STARTDATETIME) % 30)
order by 1;
谢谢:)
答案 0 :(得分:0)
DATEPART(MINUTE,...)
只是一小时内的会议记录,而不是一天内的会议记录。考虑小时数
select (60 * DATEPART(HOUR, usr.STARTDATETIME) +
DATEPART(MINUTE, usr.STARTDATETIME)) % 30, COUNT(*)
...
答案 1 :(得分:0)
我建议使用递归CTE生成所有30分钟的时间段,然后在桌面上加入以查找出现的事件。
$users_count
答案 2 :(得分:0)
这将按小时(不是30分钟)分组
declare @startdatetime datetime = '2016-02-03 00:00:00'
declare @enddatetime datetime = '2016-02-19 23:59:59'
declare @apiserviceid int = 21
select min(usr.STARTDATETIME), max(usr.STARTDATETIME), count(0)
from TABLE 1 usr
where usr.APIREQUESTID = @apiserviceid
and usr.STARTDATETIME >= @startdatetime and usr.STARTDATETIME <= @enddatetime
group by convert(char(13),usr.STARTDATETIME,121)
order by 1;
答案 3 :(得分:0)
select cast(year(usr.STARTDATETIME) as varchar)+''
+cast(month(usr.STARTDATETIME) as varchar)+
'-'+cast(day(usr.STARTDATETIME) as varchar)+' '+CAST(DATEPART(HOUR, usr.STARTDATETIME) AS VARCHAR)+':'+
CASE WHEN DATEPART(MINUTE, usr.STARTDATETIME)>30 THEN '31 - '+
CAST((DATEPART(HOUR, usr.STARTDATETIME)+1) AS VARCHAR) +':00' ELSE '00 -
'+CAST(DATEPART(HOUR, usr.STARTDATETIME) AS VARCHAR)+':30' END , COUNT(*)
from TABLE 1 usr
where usr.APIREQUESTID = @apiserviceid
and usr.STARTDATETIME >= @startdatetime and usr.STARTDATETIME <= @enddatetime
group by
cast(year(usr.STARTDATETIME) as varchar)+'-'+
cast(month(usr.STARTDATETIME) as varchar)+'-'+cast(day(usr.STARTDATETIME) as
varchar)+' '+CAST(DATEPART(HOUR, usr.STARTDATETIME) AS VARCHAR)+':'+
CASE WHEN DATEPART(MINUTE, usr.STARTDATETIME)>30 THEN '31 - '+
CAST((DATEPART(HOUR, usr.STARTDATETIME)+1) AS VARCHAR) +':00' ELSE '00 -
'+CAST(DATEPART(HOUR, usr.STARTDATETIME) AS VARCHAR)+':30' END
order by 1;