I have a table in SQL with a column Recordtime and valueasreal
RecordTime ValueAsReal
2015-11-30 12:59:00 195,6907
2015-11-30 13:00:00 203,8667
2015-11-30 13:01:00 198,0907
2015-11-30 13:02:00 198,3573
2015-11-30 13:03:00 208,064
2015-11-30 13:04:00 211,3066
2015-11-30 13:05:00 219,36
2015-11-30 13:06:00 233,4453
2015-11-30 13:07:00 232,8106
2015-11-30 13:08:00 217,872
2015-11-30 13:09:00 214,464
2015-11-30 13:10:00 200,0587
2015-11-30 13:11:00 173,776
2015-11-30 13:12:00 181,1787
I need to find the first 5 minute interval where the valueasreal >= 200
, in this case the query would return
2015-11-30 13:03:00 208,064
2015-11-30 13:04:00 211,3066
2015-11-30 13:05:00 219,36
2015-11-30 13:06:00 233,4453
2015-11-30 13:07:00 232,8106
2015-11-30 13:08:00 217,872
OR
StartRecordTime EndRecordTime AVG(ValueAsreal)
2015-11-30 13:03:00 2015-11-30 13:08:00 220.476
Best Answer
You can find the min value for
RecordTime
where there are 6 consecutive rows by using a correlated query doingtop(6)
and then count the number of rows that is greater than or equal to200
.Use that min value for
RecordTime
against the table to get the 6 consecutive rows that you want.Query plan: