我下面的ms-sql代码在sql server 2017上运行:
DECLARE @report_int TABLE
(
atimestamp DATETIME,
adate DATE,
ahour INT,
intvalue INT,
attribute CHAR(254),
total INT
)
INSERT INTO @report_int
SELECT
el.servertimestamp,
cast(el.servertimestamp as date) adate,
(DATEPART(HH,el.servertimestamp)) ahour,
el.intvalue,
el.attributes,
0
FROM
eventlog el
where
el.servertimestamp BETWEEN '6/29/2017'
AND DATEADD(dd, +1, '6/29/2019')
and (attributes LIKE '%N<=>OlympieioIn%' OR attributes LIKE '%N<=>OlympieioOut%')
order by
el.servertimestamp
DECLARE @report TABLE
(
adate datetime,
ahour int,
came_in bigint,
gone_out bigint
)
--fill report with dates and hours
INSERT INTO @report (adate, ahour) SELECT DISTINCT adate, ahour FROM @report_int
--reset
UPDATE @report set came_in = 0, gone_out = 0
--update in totals, min and max times
UPDATE @report SET came_in = i.total from
@report r,
(
select adate, ahour, COUNT(*) total
FROM @report_int
WHERE attribute LIKE 'N<=>OlympieioIn'
GROUP BY adate, ahour
) i
WHERE
i.adate = r.adate
and i.ahour = r.ahour
--update out totals
UPDATE @report SET gone_out = i.total from
@report r,
(
SELECT adate, ahour, COUNT(*) total
FROM @report_int
WHERE attribute LIKE 'N<=>OlympieioOut'
GROUP BY adate, ahour
) i
where
i.adate = r.adate
and i.ahour = r.ahour
select
adate,
ahour,
RIGHT('00'+cast(ahour as varchar),2)+':00 - '+RIGHT('00'+cast(ahour+1 as varchar),2)+':00' timeframe,
came_in,
gone_out
from
@report
此代码显示每天有多少人进出建筑物。如您所见,进出的人数按小时分组显示。 下面是执行代码的屏幕截图。
我想做的是将这些数据分为2小时的分组,例如,上面执行的代码的第3和第4小时的分组将是:
日期(日期):2018-05-12 小时(小时): 15 时间范围::15:00-17:00 came_in: 0 gone_out: 2
我们将非常感谢您的帮助
谢谢。
答案 0 :(得分:0)
我只需手动创建一个时间表并将其加入。 该时间表将有24行(每小时一排),可能如下所示: 小时小时时间框架twohourtimeframe 13 13:00-14:00 13:00-15:00
答案 1 :(得分:0)
将日期时间设置为最后2小时的一种方法是使用:
DATEADD(HOUR, DATEDIFF(HOUR, 0, YourDateColumn) - (DATEDIFF(HOUR, 0, YourDateColumn) % 2), 0)
如果我们随后获取一些(随机)样本数据,则会得到以下信息:
WITH VTE AS (
SELECT CONVERT(datetime2(0),V.DT) AS DT
FROM (VALUES('2018-07-04T00:11:32'),('2018-07-04T01:17:12'),('2018-07-04T02:00:01'),('2018-07-04T07:49:59'),('2018-07-04T07:59:58'),('2018-07-04T08:00:00')) V(DT))
SELECT DT, DATEADD(HOUR, DATEDIFF(HOUR, 0, DT) - (DATEDIFF(HOUR, 0, DT) % 2), 0) AS DT2h
FROM VTE;
哪个返回:
DT DT2h
--------------------------- -----------------------
2018-07-04 00:11:32 2018-07-04 00:00:00.000
2018-07-04 01:17:12 2018-07-04 00:00:00.000
2018-07-04 02:00:01 2018-07-04 02:00:00.000
2018-07-04 07:49:59 2018-07-04 06:00:00.000
2018-07-04 07:59:58 2018-07-04 06:00:00.000
2018-07-04 08:00:00 2018-07-04 08:00:00.000
如果您希望将COUNT
放2小时:
WITH VTE AS (
SELECT CONVERT(datetime2(0),V.DT) AS DT
FROM (VALUES('2018-07-04T00:11:32'),('2018-07-04T01:17:12'),('2018-07-04T02:00:01'),('2018-07-04T07:49:59'),('2018-07-04T07:59:58'),('2018-07-04T08:00:00')) V(DT))
SELECT DATEADD(HOUR, DATEDIFF(HOUR, 0, DT) - (DATEDIFF(HOUR, 0, DT) % 2), 0) AS DT2h, COUNT(DT) AS C
FROM VTE
GROUP BY DATEADD(HOUR, DATEDIFF(HOUR, 0, DT) - (DATEDIFF(HOUR, 0, DT) % 2), 0);
哪个返回:
DT2h C
----------------------- -----------
2018-07-04 00:00:00.000 2
2018-07-04 02:00:00.000 1
2018-07-04 06:00:00.000 2
2018-07-04 08:00:00.000 1