简体   繁体   中英

Aggregating (group) time by 15 seconds intervals // Microsoft SQL Server 2019

I'm new to Microsoft SQL Server 2019. Currently I'm trying to aggregate Timestamp (type datetime ) by 15 second intervals.

Desired result:

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

I've tried to convert "1 minute interval" from

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'

to

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'

and faced with an issue

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.

Then I've tried to use 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'

and got error

Arithmetic overflow error converting expression to data type int.

I've tried to use

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';

but I have these result

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

instead of desired

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

It should start from the first 15 second group

You can use 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM