Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按15秒间隔聚合(组)时间//Microsoft SQL Server 2019_Sql_Sql Server_Date_Datetime_Aggregate - Fatal编程技术网

按15秒间隔聚合(组)时间//Microsoft SQL Server 2019

按15秒间隔聚合(组)时间//Microsoft SQL Server 2019,sql,sql-server,date,datetime,aggregate,Sql,Sql Server,Date,Datetime,Aggregate,我是2019年Microsoft SQL Server的新手。目前,我正在尝试按15秒的间隔聚合时间戳(键入日期时间) 预期结果: 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-

我是2019年Microsoft SQL Server的新手。目前,我正在尝试按15秒的间隔聚合
时间戳
(键入
日期时间

预期结果:

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分钟间隔” 从

然后我尝试使用
datediff\u 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'
我试着用

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: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()

选择时间戳,
datetimefromparts(年(时间戳)、月(时间戳)、日(时间戳),
日期部分(小时、时间戳),
日期部分(分钟、时间戳),
(上限(日期部分(秒,时间戳))/15)*15,
0
)如图2所示
从tags.dbo.jan
其中ValueID='349'和
时间戳<'2019-01-01 04:02:00.000';

此解决方案可能会对您有所帮助。。这是可行的,但不是我想要感谢你回答的方式!我将更新问题1。我认为有
datepart(第二个,时间戳)
被遗漏了2。分组应从第一个15秒组开始
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
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;