[英]Aggregating (group) time by 15 seconds intervals // Microsoft SQL Server 2019
我是 Microsoft SQL Server 2019 的新手。目前我正在尝试以 15 秒的间隔聚合Timestamp
(类型datetime
)。
期望的结果:
Timestamp Timestamp2
2019-01-01 04:00:00.487 2019-01-01 04:00:15.000
2019-01-01 04:00:01.487 2019-01-01 04:00:15.000
2019-01-01 04:00:02.487 2019-01-01 04:00:15.000
2019-01-01 04:00:03.487 2019-01-01 04:00:15.000
2019-01-01 04:00:04.487 2019-01-01 04:00:15.000
2019-01-01 04:00:05.487 2019-01-01 04:00:15.000
2019-01-01 04:00:06.487 2019-01-01 04:00:15.000
2019-01-01 04:00:07.487 2019-01-01 04:00:15.000
2019-01-01 04:00:08.487 2019-01-01 04:00:15.000
2019-01-01 04:00:09.487 2019-01-01 04:00:15.000
2019-01-01 04:00:10.487 2019-01-01 04:00:15.000
2019-01-01 04:00:11.487 2019-01-01 04:00:15.000
2019-01-01 04:00:12.487 2019-01-01 04:00:15.000
2019-01-01 04:00:13.487 2019-01-01 04:00:15.000
2019-01-01 04:00:14.487 2019-01-01 04:00:15.000
2019-01-01 04:00:15.487 2019-01-01 04:00:30.000
2019-01-01 04:00:16.487 2019-01-01 04:00:30.000
2019-01-01 04:00:17.487 2019-01-01 04:00:30.000
2019-01-01 04:00:18.487 2019-01-01 04:00:30.000
2019-01-01 04:00:19.487 2019-01-01 04:00:30.000
我试图将“1分钟间隔”从
Select
Timestamp,
dateadd(MINUTE, 1+datediff(MINUTE, 0, [Timestamp]), 0) AS Timestamp2
FROM tags.dbo.jan
where ValueID = '349' and Timestamp < '2019-01-01 04:02:00.000'
至
Select
Timestamp,
dateadd(second, 15+datediff(second, 0, [Timestamp]), 0) AS Timestamp2
FROM tags.dbo.jan
where ValueID = '349' and Timestamp < '2019-01-01 04:02:00.000'
并面临一个问题
The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use datediff with a less precise datepart.
然后我尝试使用datediff_big
Select
Timestamp,
dateadd(second, 15+datediff_big(second, 0, [Timestamp]), 0) AS Timestamp2
FROM tags.dbo.jan
where ValueID = '349' and Timestamp < '2019-01-01 04:02:00.000'
并得到错误
Arithmetic overflow error converting expression to data type int.
我试过用
Select Timestamp,
datetimefromparts(year(Timestamp), month(Timestamp), day(Timestamp),
datepart(hour, Timestamp),
datepart(minute, Timestamp),
(ceiling(datepart(second, Timestamp)) / 15) * 15,
0
) as timestamp2
FROM jan.dbo.jan
where ValueID = '349' and
Timestamp < '2019-01-01 04:02:00.000';
但我有这些结果
2019-01-01 04:00:12.487 2019-01-01 04:00:00.000
2019-01-01 04:00:13.487 2019-01-01 04:00:00.000
2019-01-01 04:00:14.487 2019-01-01 04:00:00.000
2019-01-01 04:00:15.487 2019-01-01 04:00:15.000
2019-01-01 04:00:16.487 2019-01-01 04:00:15.000
2019-01-01 04:00:17.487 2019-01-01 04:00:15.000
而不是想要的
2019-01-01 04:00:12.487 2019-01-01 04:00:15.000
2019-01-01 04:00:13.487 2019-01-01 04:00:15.000
2019-01-01 04:00:14.487 2019-01-01 04:00:15.000
2019-01-01 04:00:15.487 2019-01-01 04:00:30.000
2019-01-01 04:00:16.487 2019-01-01 04:00:30.000
2019-01-01 04:00:17.487 2019-01-01 04:00:30.000
应该从前15秒组开始
您可以使用datetimefromparts()
:
Select Timestamp,
datetimefromparts(year(Timestamp), month(Timestamp), day(Timestamp),
datepart(hour, Timestamp),
datepart(minute, Timestamp),
(ceiling(datepart(second, Timestamp)) / 15) * 15,
0
) as timestamp2
FROM tags.dbo.jan
where ValueID = '349' and
Timestamp < '2019-01-01 04:02:00.000';
declare @t table(thetimestamp datetime);
insert into @t(thetimestamp)
values('20190101 04:00:00.487'), ('20190101 04:00:02.487'),
('20190101 04:00:15.487'), ('20190101 04:00:20.487'),
('20190101 04:00:30.487'), ('20190101 04:00:35.487'),
('20190101 04:00:45.487'), ('20190101 04:00:57.487'),
--
('20190101 04:00:00.000'), ('20190101 04:00:15.000'), ('20190101 04:00:30.000'), ('20190101 04:00:45.000');
select *,
--add ms to next 15sec boundary
dateadd(millisecond,
(15000-((1000*datepart(second, thetimestamp)+ datepart(millisecond, thetimestamp))%15000))%15000,
thetimestamp) as upper15sec,
--subtract ms from previous 15sec boundary
dateadd(millisecond,
-(15000+((1000*datepart(second, thetimestamp)+ datepart(millisecond, thetimestamp))%15000))%15000,
thetimestamp) as lower15sec
from @t;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.