22,210
社区成员
发帖
与我相关
我的任务
分享
--开始查询
;with
cte as(
select *,case when (select count(1) from #a where id=a.id-1 and
datediff(s,[time],a.[time])>60 )=0 and a.[value]>10 then 10 else 11 end
as t from #a a
) , cc as
(
select * from cte a where t=10 and
exists (select 1 from cte where t=11 and datediff(s,[time],a.[time]) <=120 and
datediff(s,[time],(select [time] from cte where id=a.id+1))>120)
or
exists(select 1 from cte where t=11 and a.id=id-1 and [value]>10)
or a.id=(select max(id) from cte where t=10)
)
select rank() over (order by id) as id,code,value,time from cc
结果集中还应该有一条 4 3101 16 2014-02-21 11:08:49 2014-02-21 11:10:01 表中ID16-18被丢弃,后面的重新计算得出一条
--开始查询
;with cte as(
select *,case when (select count(1) from #a where id=a.id-1 and
datediff(s,[time],a.[time])>60 )=0 and a.[value]>10 then 10 else 11 end
as t from #a a
) , cc as( select * from cte a where t=10 and
exists (select 1 from cte where t=11 and datediff(s,[time],a.[time]) <=120 and
datediff(s,[time],(select [time] from cte where id=a.id+1))>120)
union all
select * from cte a where t=10 and
exists(select 1 from cte where t=11 and a.id=id-1 and [value]>10)
union all
select * from cte a where a.id=(select max(id) from cte where t=10)
and datediff(s,(select max(Convert(varchar,[time],120)) from cte where t=11),a.[time])>60
)
select rank() over (order by id) as id,code,value,time from cc
--查询结果
/*
id code value time
-------------------- ----------- ----------- --------------------
1 3101 13 2014-02-21 10:09:08
2 3101 13 2014-02-21 10:22:22
3 3101 17 2014-02-21 10:24:08
4 3101 16 2014-02-21 11:10:01
(4 行受影响)
*/
if object_id('tempdb..#a') is not null drop table #a
go
create table #a
(
id int,
code int ,
[value] int,
[time] varchar(20)
)
insert into #a
select 1,3101,13,'2014-02-21 10:08:06' union all
select 2,3101,15,'2014-02-21 10:08:41' union all
select 3,3101,12,'2014-02-21 10:08:48' union all
select 4,3101,13,'2014-02-21 10:09:08' union all
select 5,3101,14,'2014-02-21 10:20:55' union all
select 6,3101,17,'2014-02-21 10:21:22' union all
select 7,3101,19,'2014-02-21 10:21:33' union all
select 8,3101,13,'2014-02-21 10:21:58' union all
select 9,3101,13,'2014-02-21 10:22:08' union all
select 10,3101,13,'2014-02-21 10:22:22' union all
select 11,3101,12,'2014-02-21 10:23:01' union all
select 12,3101,13,'2014-02-21 10:23:12' union all
select 13,3101,15,'2014-02-21 10:23:22' union all
select 14,3101,13,'2014-02-21 10:23:45' union all
select 15,3101,17,'2014-02-21 10:24:08' union all
select 16,3101,13,'2014-02-21 11:08:06' union all
select 17,3101,13,'2014-02-21 11:08:41' union all
select 18,3101,9,'2014-02-21 11:08:44' union all
select 19,3101,12,'2014-02-21 11:08:49' union all
select 20,3101,13,'2014-02-21 11:09:08' union all
select 21,3101,16,'2014-02-21 11:10:01'
--开始查询
;with cte as(
select *,case when (select count(1) from #a where id=a.id-1 and
datediff(s,[time],a.[time])>60 )=0 and a.[value]>10 then 10 else 11 end
as t from #a a
) select * from cte a where t=10 and
exists (select 1 from cte where t=11 and datediff(s,[time],a.[time]) <=120 and
datediff(s,[time],(select [time] from cte where id=a.id+1))>120)
union all
select * from cte a where t=10 and
exists(select 1 from cte where t=11 and a.id=id-1 and [value]>10)
union all
select * from cte a where a.id=(select max(id) from cte where t=10)
and datediff(s,(select max(Convert(varchar,[time],120)) from cte where t=11),a.[time])>60
--查询结果
/*
id code value time t
----------- ----------- ----------- -------------------- -----------
10 3101 13 2014-02-21 10:22:22 10
4 3101 13 2014-02-21 10:09:08 10
15 3101 17 2014-02-21 10:24:08 10
21 3101 16 2014-02-21 11:10:01 10
(4 行受影响)
*/
有点复杂,也不知道直接SQL语句能不能得出想要的结果.先谢过!如下: 表A 有如下字段 ID CODE VALUE TIME 1 3101 13 2014-02-21 10:08:06 2 3101 15 2014-02-21 10:08:41 3 3101 12 2014-02-21 10:08:48 4 3101 13 2014-02-21 10:09:08 5 3101 14 2014-02-21 10:20:55 6 3101 17 2014-02-21 10:21:22 7 3101 19 2014-02-21 10:21:33 8 3101 13 2014-02-21 10:21:58 9 3101 13 2014-02-21 10:22:08 10 3101 13 2014-02-21 10:22:22 11 3101 12 2014-02-21 10:23:01 12 3101 13 2014-02-21 10:23:12 13 3101 15 2014-02-21 10:23:22 14 3101 13 2014-02-21 10:23:45 15 3101 17 2014-02-21 10:24:08 16 3101 13 2014-02-21 11:08:06 17 3101 13 2014-02-21 11:08:41 18 3101 9 2014-02-21 11:08:44 19 3101 12 2014-02-21 11:08:49 20 3101 13 2014-02-21 11:09:08 21 3101 16 2014-02-21 11:10:01 需要的一个结果为 1.如果连续60秒VALUE值都是大于10的则记录为一条,超过120秒,则记录为两条记录120秒内只记录为一条记录; 2.如果连续60秒VALUE有大于10的但是也有小于10的,只要有小于10的就从下一条大于10的开始重新计算,小于10之前的就忽略; 以上记录查询的结果为 ID CODE MAX_VALUE BEGINTIME ENDTIME 1 3101 15 2014-02-21 10:08:06 2014-02-21 10:09:08 2 3101 19 2014-02-21 10:20:55 2014-02-21 10:22:22 3 3103 17 2014-02-21 10:23:01 2014-02-21 10:24:08 结果描述: ID1为表中ID 1-4为一分钟以上没有到到120秒连续VALUE值大于10的,所以记录为一条记录 ID2和3为表中ID 5-15为连续60秒以上,且超过了120秒,且第二条也超过60秒,所以记录为两条 表中16-21因为不满足连续VALUE都大于10,所以不算入统计.