以15秒为间隔的聚合(组)时间// Microsoft SQL Server 2019

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了以15秒为间隔的聚合(组)时间// 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, 1619444420), 0) AS Timestamp2 
FROM tags.dbo.jan
where ValueID = '349' and Timestamp < '2019-01-01 04:02:00.000'

to

Select   
Timestamp, 
dateadd(second, 15+datediff(second, 0, 1619444420), 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, 1619444420), 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(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;

以上是关于以15秒为间隔的聚合(组)时间// Microsoft SQL Server 2019的主要内容,如果未能解决你的问题,请参考以下文章

以秒为单位将时间间隔格式化为 X 小时(秒) Y 分钟(秒) Z 秒(秒)

在 Oracle SQL 中按时间间隔聚合数据

MongoDB 时间序列聚合

如果我得到 15 分钟的间隔数据来预测每小时目标,我应该使用 15 分钟的数据还是聚合到 1 小时的数据进行训练?

mysql查询两个日期的时间间隔,以秒为单位

聚合 + 最后 & 第一个 -> 丢失订单