日期:2014-05-16  浏览次数:20626 次

如何查询记录中超过30度和低于30度的那个点的记录?
表是个流水的记录,假设每5秒记录一次当前的温度 ,如下:

TimePoint                    TempValue    
20131201080100       20
20131201080105       27
20131201080110       31
20131201080115       32
20131201080120       28
20131201080125       20
20131201080130       20
20131201080135       40
20131201080140       35
20131201080145       20

得到数据  
20131201080100       20   低
20131201080110       31   高
20131201080120       28   低
20131201080135       40   高
20131201080145       20   低 


中间隔多少条记录不一定,就是要看那个临界的记录,第一次高于30度,或者低于30度的那条记录。

这种查询,因个人水平问题,除了循环判断,没有太好的想法,求大神指点。



------解决方案--------------------

with tb(TimePoint,TempValue)as(
select 20131201080100,       20 union
select 20131201080105,       27 union
select 20131201080110,       31 union
select 20131201080115,       32 union
select 20131201080120,       28 union
select 20131201080125,       20 union
select 20131201080130,       20 union
select 20131201080135,       40 union
select 20131201080140,       35 union
select 20131201080145,       20)
,tc as(
select timepoint,tempvalue,row_number() over(order by timepoint)num from tb)
select * from tc a 
where not exists(select 1 from tc where a.num-1=num and TempValue/30=a.TempValue/30)