我正在尝试根据开始时间和结束时间以及时间间隔从表中过滤数据 例如
2018-07-18 10:00:00 : 01
2018-07-18 10:00:01 : 02
2018-07-18 10:00:02 : 03
2018-07-18 10:00:03 : 04
2018-07-18 10:00:04 : 05
2018-07-18 10:00:05 : 06
2018-07-18 10:00:06 : 07
2018-07-18 10:00:07 : 08
我希望每2秒显示一次数据
2018-07-18 10:00:00 : 01
2018-07-18 10:00:02 : 03
2018-07-18 10:00:04 : 05
2018-07-18 10:00:06 : 07
答案 0 :(得分:0)
假设您正在使用SQL Server
您可以按照以下步骤进行操作-
declare @datetime as table
( dt datetime)
declare @start as datetime = '2018-07-18 10:00:02 : 03'; --arbitrary - choose what you want
declare @end as datetime = '2018-07-18 10:00:06 : 07'; --arbitrary - choose what you want
insert into @datetime
values
('2018-07-18 10:00:00 : 01')
,('2018-07-18 10:00:01 : 02')
,('2018-07-18 10:00:02 : 03')
,('2018-07-18 10:00:03 : 04')
,('2018-07-18 10:00:04 : 05')
,('2018-07-18 10:00:05 : 06')
,('2018-07-18 10:00:06 : 07')
,('2018-07-18 10:00:07 : 08')
select * from @datetime
where
dt>= @start and dt <=@end -- choose the condition you want
and datepart(SS,dt)%2 = 0 -- %2 is for 2 seconds; modify interval accordingly
输出
2018-07-18 10:00:02.003
2018-07-18 10:00:04.007
2018-07-18 10:00:06.007
答案 1 :(得分:0)
您可以尝试以下方法:
DECLARE @interval INT=2;
DECLARE @start AS DATETIME = '2018-07-18 10:00:00 : 03';
DECLARE @datetime AS TABLE ( dt DATETIME)
INSERT INTO @datetime
VALUES
('2018-07-18 10:00:00 : 01')
,('2018-07-18 10:00:01 : 02')
,('2018-07-18 10:00:02 : 03')
,('2018-07-18 10:00:03 : 04')
,('2018-07-18 10:00:04 : 05')
,('2018-07-18 10:00:05 : 06')
,('2018-07-18 10:00:06 : 07')
,('2018-07-18 10:00:07 : 08')
;WITH cte
AS
(
SELECT *
FROM @datetime
WHERE CONVERT(CHAR(19),CONVERT(DATETIME,dt,101),120)=CONVERT(CHAR(19),CONVERT(DATETIME,@start,101),120)
UNION ALL
SELECT t1.*
FROM @datetime t1
JOIN cte t2 ON CONVERT(CHAR(19),CONVERT(DATETIME,t1.dt,101),120) =
DATEADD(ss,@interval,CONVERT(CHAR(19),CONVERT(DATETIME,t2.dt,101),120))
)
SELECT * FROM cte
注意: CONVERT(CHAR(19),CONVERT(DATETIME,@ Start,101),120 )用于丢弃比较中的毫秒级零件。
答案 2 :(得分:0)
假设您每秒钟都有数据,我建议使用row_number()
:
select t.*
from (select t.*
row_number() over
(partition by dateadd(second,
2 * (datediff(second, 0, dtcol) / 2),
0
)
order by dtcol
) as seqnum
from t
) t
where seqnum = 1;