簡體   English   中英

以 15 秒為間隔聚合(組)時間 // Microsoft SQL Server 2019

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM