简体   繁体   English

MS SQL:每15分钟将数据流筛选为第一个值

[英]MS SQL: Filter datastream to first value every 15 minutes

I would like to apply a filter to filter the data to 15 minutes, with only 1 row for every 15 minutes returned. 我想应用一个过滤器将数据过滤到15分钟,每15分钟返回1行。

I have 2 tables data, each table contains 2 columns: "Tijd" as timestamp, "Kanaal 1" as float. 我有2个表数据,每个表包含2列:“ Tijd”作为时间戳,“ Kanaal 1”作为浮点。 A new row is added to both tables based on the frequency of the program (Table 1) or a external trigger (Table 2). 根据程序的频率(表1)或外部触发器(表2),将新行添加到两个表中。

My current code works on the first table 我当前的代码适用于第一个表

select [Tijd], [Kanaal 1] 
FROM Table_Metingen 
WHERE datepart(mi,tijd) % 15 = 0

Table 1: (regulary updated) 表1 :(定期更新)

Tijd                | Kanaal 1
2016-06-27 00:00:00 | 53
2016-06-27 00:01:00 | 53
2016-06-27 00:02:00 | 53
2016-06-27 00:03:00 | 53
2016-06-27 00:04:00 | 53
2016-06-27 00:05:00 | 53
2016-06-27 00:06:00 | 53
2016-06-27 00:07:00 | 53

Tabel 2: (updated by an external trigger) 表2 :(由外部触发器更新)

Tijd                | Kanaal 1
2016-06-27 00:00:01 | 53
2016-06-27 00:01:02 | 53
2016-06-27 00:01:04 | 53
2016-06-27 00:01:10 | 53
2016-06-27 00:02:04 | 53
2016-06-27 00:05:03 | 53
2016-06-27 00:06:02 | 53
2016-06-27 00:10:01 | 53

Output of current code would be as following: Table 1: (regulary updated) 当前代码的输出如下:表1 :(定期更新)

Tijd                | Kanaal 1
2016-06-27 00:00:00 | 53
2016-06-27 00:15:00 | 53
2016-06-27 00:30:00 | 53
2016-06-27 00:45:00 | 53
2016-06-27 01:00:00 | 53
2016-06-27 01:15:00 | 53
2016-06-27 01:30:00 | 53
2016-06-27 01:45:00 | 53

Tabel 2: (updated by an external trigger) 表2 :(由外部触发器更新)

Tijd                | Kanaal 1
2016-06-27 00:00:01 | 53
2016-06-27 00:15:02 | 53
2016-06-27 00:30:04 | 53
2016-06-27 00:45:00 | 53
2016-06-27 00:45:2  | 53 < Extra row, not needed
2016-06-27 01:00:01 | 53
2016-06-27 01:15:03 | 53
2016-06-27 01:30:01 | 53
2016-06-27 01:30:05 | 53 < Extra row, not needed
2016-06-27 01:45:02 | 53

The additional rows are due to the seconds in your Tijd column. 额外的行归因于Tijd列中的秒数。 01:30:01 and 01:30:05 both fullfill the check minutes%15 = 0. So either you get rid of the seconds in your query or you use a cte with a row_number() and only select all rows with "rownum = 1" (as example). 01:30:01和01:30:05都填写了检查分钟数%15 =0。因此,您可以摆脱查询中的秒数,或者使用具有row_number()的cte并仅选择所有带有“ rownum”的行= 1英寸(例如)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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