时间 val
2011-1-1 0:10:01 0.1
2011-1-1 0:20:01 0.5
2011-1-1 0:30:01 1.1
2011-1-1 0:40:01 1.3
2011-1-1 0:50:01 1.5
2011-1-1 1:00:01 1.7
2011-1-1 1:10:01 0.8
2011-1-1 1:20:01 1.6
2011-1-1 1:30:01 0.8查出连续时间段内val超过1并大于3次的语句,希望高手指教
2011-1-1 0:10:01 0.1
2011-1-1 0:20:01 0.5
2011-1-1 0:30:01 1.1
2011-1-1 0:40:01 1.3
2011-1-1 0:50:01 1.5
2011-1-1 1:00:01 1.7
2011-1-1 1:10:01 0.8
2011-1-1 1:20:01 1.6
2011-1-1 1:30:01 0.8查出连续时间段内val超过1并大于3次的语句,希望高手指教
解决方案 »
- 表与表之间的触发器怎么写
- 如何远程登陆SQL SERVER 2000
- MSSQL2008的事件跟踪在哪里?
- 求一sql 语句??
- Bulk insert 遇到问题,求解?
- 大家帮忙看看这个表的键如何设计?
- 表中有一个time字段,是datetime类型,现在要把Getdate()减去那个字段的时间后返回秒数,sql怎么写?
- SQL的排序问题
- 有谁用过word的邮件合并功能。lujun,net_steven,icevi快帮忙。
- 关于复合主键和自增长主键设计 以及 增删改 性能 讨论
- mysql主键自动增加
- 逻辑上有点混乱,希望达人指点(主要是数据库的设计上,由一个用户一组变为一用户多组时有点乱了...)
insert into @t
select '2011-1-1 0:10:01',0.1 union all
select '2011-1-1 0:20:01',0.5 union all
select '2011-1-1 0:30:01',1.1 union all
select '2011-1-1 0:40:01',1.3 union all
select '2011-1-1 0:50:01',1.5 union all
select '2011-1-1 1:00:01',1.7 union all
select '2011-1-1 1:10:01',0.8 union all
select '2011-1-1 1:20:01',1.6 union all
select '2011-1-1 1:30:01',0.8;with t1 as
(
select row_number() over(order by t) as r, t,case when v>1 then 1 else 0 end as v from @t
),
t2 as
(
select * from t1 where v = 1
),
t3 as
(
select row_number() over(order by r) as r2, * from t2
)
,
t4 as
(
select r-r2 as g,t,v
from t3
)select min(t) as st,max(t) as et,count(g) as times from t4
group by g
insert into tb values('2011-1-1 0:10:01', 0.1)
insert into tb values('2011-1-1 0:20:01', 0.5)
insert into tb values('2011-1-1 0:30:01', 1.1)
insert into tb values('2011-1-1 0:40:01', 1.3)
insert into tb values('2011-1-1 0:50:01', 1.5)
insert into tb values('2011-1-1 1:00:01', 1.7)
insert into tb values('2011-1-1 1:10:01', 0.8)
insert into tb values('2011-1-1 1:20:01', 1.6)
insert into tb values('2011-1-1 1:30:01', 0.8)
go
select min(dt) 开始时间, max(dt) 结束时间 from
(
select m.* , m.px - ((select count(1) from
(
select t.* ,(select count(1) from tb where dt < t.dt) + 1 px from tb t
) n where val > 1 and n.px < m.px) + 1) px2 from
(
select t.* ,(select count(1) from tb where dt < t.dt) + 1 px from tb t
) m
) k
group by px2 having count(1) >= 4drop table tb/*
开始时间 结束时间
------------------------------------------------------ ------------------------------------------------------
2011-01-01 00:30:01.000 2011-01-01 01:10:01.000(所影响的行数为 1 行)
*/
insert into tb values('2011-1-1 0:10:01', 0.1)
insert into tb values('2011-1-1 0:20:01', 0.5)
insert into tb values('2011-1-1 0:30:01', 1.1)
insert into tb values('2011-1-1 0:40:01', 1.3)
insert into tb values('2011-1-1 0:50:01', 1.5)
insert into tb values('2011-1-1 1:00:01', 1.7)
insert into tb values('2011-1-1 1:10:01', 0.8)
insert into tb values('2011-1-1 1:20:01', 1.6)
insert into tb values('2011-1-1 1:30:01', 0.8)
goselect min(dt) 开始时间, max(dt) 结束时间 from
(
select m.* , m.px - ((select count(1) from
(
select t.* ,(select count(1) from tb where dt < t.dt) + 1 px from tb t
) n where n.val > 1 and n.px < m.px) + 1) px2 from
(
select t.* ,(select count(1) from tb where dt < t.dt) + 1 px from tb t
) m where m.val > 1) k
group by px2 having count(1) >= 3drop table tb/*
开始时间 结束时间
------------------------------------------------------ ------------------------------------------------------
2011-01-01 00:30:01.000 2011-01-01 01:00:01.000(所影响的行数为 1 行)
*/
create table tb(dt datetime,val decimal(18,2))
insert into tb values('2011-1-1 0:10:01', 0.1)
insert into tb values('2011-1-1 0:20:01', 0.5)
insert into tb values('2011-1-1 0:30:01', 1.1)
insert into tb values('2011-1-1 0:40:01', 1.3)
insert into tb values('2011-1-1 0:50:01', 1.5)
insert into tb values('2011-1-1 1:00:01', 1.7)
insert into tb values('2011-1-1 1:10:01', 0.8)
insert into tb values('2011-1-1 1:20:01', 1.6)
insert into tb values('2011-1-1 1:30:01', 0.8)
goselect t1.dt , t1.val from tb t1,
(
select min(dt) 开始时间, max(dt) 结束时间 from
(
select m.* , m.px - ((select count(1) from
(
select t.* ,(select count(1) from tb where dt < t.dt) + 1 px from tb t
) n where n.val > 1 and n.px < m.px) + 1) px2 from
(
select t.* ,(select count(1) from tb where dt < t.dt) + 1 px from tb t
) m where m.val > 1) k
group by px2 having count(1) >= 3
) t2
where t1.dt between t2.开始时间 and t2.结束时间
drop table tb/*
dt val
------------------------------------------------------ --------------------
2011-01-01 00:30:01.000 1.10
2011-01-01 00:40:01.000 1.30
2011-01-01 00:50:01.000 1.50
2011-01-01 01:00:01.000 1.70(所影响的行数为 4 行)
*/
insert into tb values('2011-1-1 0:10:01', 0.1)
insert into tb values('2011-1-1 0:20:01', 0.5)
insert into tb values('2011-1-1 0:30:01', 1.1)
insert into tb values('2011-1-1 0:40:01', 1.3)
insert into tb values('2011-1-1 0:50:01', 1.5)
insert into tb values('2011-1-1 1:00:01', 1.7)
insert into tb values('2011-1-1 1:10:01', 0.8)
insert into tb values('2011-1-1 1:20:01', 1.6)
insert into tb values('2011-1-1 1:30:01', 0.8)
goselect t1.dt , t1.val from tb t1,
(
select min(dt) 开始时间, max(dt) 结束时间 from
(
select m.* , m.px - ((select count(1) from
(
select t.* ,(select count(1) from tb where dt < t.dt) + 1 px from tb t
) n where n.val > 1 and n.px < m.px) + 1) px2 from
(
select t.* ,(select count(1) from tb where dt < t.dt) + 1 px from tb t
) m where m.val > 1) k
group by px2 having count(1) > 3
) t2
where t1.dt between t2.开始时间 and t2.结束时间
drop table tb/*
dt val
------------------------------------------------------ --------------------
2011-01-01 00:30:01.000 1.10
2011-01-01 00:40:01.000 1.30
2011-01-01 00:50:01.000 1.50
2011-01-01 01:00:01.000 1.70(所影响的行数为 4 行)
*/
*,
case
when (select case when count(1)=3 then 1 else 0 end from (select top 3 * from tb where 时间<=a.时间 order by 时间 desc) S where val>1)=1
or (select case when count(1)=3 then 1 else 0 end from (select top 3 * from tb where 时间>=a.时间 order by 时间 ) S where val>1)=1
then 1
else 0
end
是否连续
from tb A
create table tb(dt datetime,val decimal(18,2))
insert into tb values('2011-1-1 0:10:01', 0.1)
insert into tb values('2011-1-1 0:20:01', 0.5)
insert into tb values('2011-1-1 0:30:01', 1.1)
insert into tb values('2011-1-1 0:40:01', 1.3)
insert into tb values('2011-1-1 0:50:01', 1.5)
insert into tb values('2011-1-1 1:00:01', 1.7)
insert into tb values('2011-1-1 1:10:01', 0.8)
insert into tb values('2011-1-1 1:20:01', 1.6)
insert into tb values('2011-1-1 1:30:01', 0.8)select
*,
case
when (select case when count(1)=3 then 1 else 0 end from (select top 3 * from tb where dt<=a.dt order by dt desc) S where val>1)=1
or (select case when count(1)=3 then 1 else 0 end from (select top 3 * from tb where dt>=a.dt order by dt ) S where val>1)=1
then 1
else 0
end
是否连续
from tb Adt val 是否连续
----------------------- ------- -
2011-01-01 00:10:01.000 0.10 0
2011-01-01 00:20:01.000 0.50 0
2011-01-01 00:30:01.000 1.10 1
2011-01-01 00:40:01.000 1.30 1
2011-01-01 00:50:01.000 1.50 1
2011-01-01 01:00:01.000 1.70 1
2011-01-01 01:10:01.000 0.80 0
2011-01-01 01:20:01.000 1.60 0
2011-01-01 01:30:01.000 0.80 0