34,588
社区成员
发帖
与我相关
我的任务
分享
with tb(TimePoint,TempValue,PF)as(
select 20131201080100,20.02,0.2 union
select 20131201080105,27.33,1.2 union
select 20131201080110,31.36,1.4 union
select 20131201080115,32.00,1.8 union
select 20131201080120,28.59,0.2 union
select 20131201080125,50.26,1.4 union
select 20131201080130,20.34,1.6 union
select 20131201080135,40.18,1.5 union
select 20131201080140,35.00,1.0 union
select 20131201080145,20.21,0.2 union
select 20131201080150,20.54,0.2 union
select 20131201080155,31.22,0.9 )
select * from tb
DROP TABLE #t
GO
with tb(TimePoint,TempValue,PF) AS (
select 20131201080100,20.02,0.2 union
select 20131201080105,27.33,1.2 union
select 20131201080110,31.36,1.4 union
select 20131201080115,32.00,1.8 union
select 20131201080120,28.59,0.2 union
select 20131201080125,50.26,1.4 union
select 20131201080130,20.34,1.6 union
select 20131201080135,40.18,1.5 union
select 20131201080140,35.00,1.0 union
select 20131201080145,20.21,0.2 union
select 20131201080150,20.54,0.2 union
select 20131201080155,31.22,0.9 )
--产生中间数据
SELECT timepoint , CASE WHEN TempValue BETWEEN 30 AND 50 AND PF BETWEEN 1 AND 2 THEN N'开' ELSE N'关' END AS kg, tempvalue INTO #t FROM tb
with b as(SELECT * , lag(kg , 1) OVER(ORDER BY timepoint) AS kg1 FROM #t)
--执行查询
SELECT * FROM b
WHERE kg1 IS NULL OR kg <> kg1
--结果
timepoint kg tempvalue kg1
--------------------------------------- ---- --------------------------------------- ----
20131201080100 关 20.02 NULL
20131201080110 开 31.36 关
20131201080120 关 28.59 开
20131201080135 开 40.18 关
20131201080145 关 20.21 开
(5 row(s) affected)
if object_id('tempdb..#a') is not null drop table #a
create table #a (TimePoint varchar(50),TempValue float,PF float)
insert into #a
select 20131201080100,20.02,0.2 union
select 20131201080105,27.33,1.2 union
select 20131201080110,31.36,1.4 union
select 20131201080115,32.00,1.8 union
select 20131201080120,28.59,0.2 union
select 20131201080125,50.26,1.4 union
select 20131201080130,20.34,1.6 union
select 20131201080135,40.18,1.5 union
select 20131201080140,35.00,1.0 union
select 20131201080145,20.21,0.2 union
select 20131201080150,20.54,0.2 union
select 20131201080155,31.22,0.9
-- select * from #a
---开始查询
;with cte as(
select * , row_number() over (order by getdate()) num,
ison=(case when TempValue between 30.00 and 50.00 and PF between 1.0 and 2.0 then 2 else 1 end)
from #a)
select TimePoint,TempValue,ison=case when ison=1 then '关' else '开' end from cte a where
not exists(select 1 from cte where num=a.num-1 and ison=a.ison)
----查询结果
TimePoint TempValue ison
-------------------------------------------------- ---------------------- ----
20131201080100 20.02 关
20131201080110 31.36 开
20131201080120 28.59 关
20131201080135 40.18 开
20131201080145 20.21 关
(5 行受影响)
SELECT C.* FROM(
SELECT TOP 1 * FROM tb ORDER BY TimePoint
UNION ALL
SELECT A.* FROM tb A,
(SELECT * FROM tb WHERE TempValue > 30 AND TempValue < 50 AND PF > 1.0 AND PF < 2.0) B
WHERE (A.TempValue < 30 OR A.TempValue > 50 OR A.PF < 1.0 OR A.PF > 2.0)
AND A.TimePoint - 5 = B.TimePoint
UNION ALL
SELECT A.* FROM tb A
(SELECT * FROM tb WHERE TempValue < 30 OR TempValue > 50 OR PF < 1.0 OR PF > 2.0) B
WHERE (A.TempValue > 30 AND A.TempValue < 50 AND A.PF > 1.0 AND A.PF < 2.0) AND A.TimePoint - 5 = B.TimePoint) C
ORDER BY TimePoint
注意:我在A.TimePoint - 5时候偷了个懒,其实应该转换成日期型扣除5秒,我只表达了扣除5秒的意思,还请自行转换