22,210
社区成员
发帖
与我相关
我的任务
分享
SELECT TOP 1000 pin,event_time,reader_state FROM acc_transaction
WHERE event_no=0
SELECT TOP 1000 用户ID=pin,开始时间='2020-07-01 10:01:00',结束时间='2020-07-01 10:05:00',间隔时间='1分钟',标志=reader_state from acc_transaction
if OBJECT_ID('tempdb..#test') is not null drop table #test
create table #test(
pin nvarchar(20),
event_time datetime,
reader_state nvarchar(1),
)
go
insert into #test
select '1' ,'2020-07-07 08:08:08','2' union all
select '1' ,'2020-07-07 08:08:08','1' union all
select '1' ,'2020-07-07 08:09:08','1' union all
select '1' ,'2020-07-07 08:09:09','1' union all
select '1' ,'2020-07-07 08:13:08','1' union all
select '1' ,'2020-07-07 08:14:08','1' union all
select '2' ,'2020-07-07 08:08:08','1' union all
select '2' ,'2020-07-07 08:11:08','1' union all
select '2' ,'2020-07-07 08:12:08','1' union all
select '2' ,'2020-07-07 08:13:08','1'
select * from #test
;with c_1 as (select
ROW_NUMBER()over(partition by pin,reader_state order by event_time)as Num,
pin,event_time,reader_state
from #test group by pin,event_time,reader_state),
c_a as (select a.Pin,a.event_time as STime,b.event_time as ETime,DATEDIFF(MINUTE,a.event_time,b.event_time)as jg,a.reader_state,a.Num as lNum,b.Num as rNum from c_1 a left join c_1 b on a.Num=b.Num-1 and a.pin=b.pin and a.reader_state=b.reader_state and DATEDIFF(MINUTE,a.event_time,b.event_time)<3),
c_2 as(select a.Pin,a.event_time as STime,b.event_time as ETime,DATEDIFF(MINUTE,a.event_time,b.event_time)as jg,a.reader_state,a.Num as lNum,b.Num as rNum from c_1 as a left join c_1 as b on a.Num=b.Num-1 and a.pin=b.pin and a.reader_state=b.reader_state and DATEDIFF(MINUTE,a.event_time,b.event_time)<3 where a.Num=1
union all
select a.* from c_2 as c join c_a as a on a.lNum=ISNULL(c.rNum,c.lNum)+1 and a.pin=c.pin and a.reader_state=c.reader_state )
select a.pin,a.STime,a.ETime ,a.jg,a.reader_state from c_2 as a
order by Pin,STime
--result
/*
1 2020-07-07 08:08:08.000 2020-07-07 08:09:08.000 1 1
1 2020-07-07 08:08:08.000 NULL NULL 2
1 2020-07-07 08:09:09.000 NULL NULL 1
1 2020-07-07 08:13:08.000 2020-07-07 08:14:08.000 1 1
2 2020-07-07 08:08:08.000 NULL NULL 1
2 2020-07-07 08:11:08.000 2020-07-07 08:12:08.000 1 1
2 2020-07-07 08:13:08.000 NULL NULL 1
*/
二十分不好赚啊select a.pin,a.STime,a.ETime ,a.jg,a.reader_state from c_2 as a
union all
select a.pin,a.event_time as STime,null as ETime ,null as jg,a.reader_state from c_1 as a where a.Num>1 and not exists(select 1 from c_2 b where a.pin=b.pin and a.reader_state=b.reader_state and((a.event_time=b.STime and a.Num=b.lNum)or((a.event_time=b.ETime and a.Num=b.rNum))))
order by Pin,STime
后边可能要改成这样if OBJECT_ID('tempdb..#test') is not null drop table #test
create table #test(
pin nvarchar(20),
event_time datetime,
reader_state nvarchar(1),
)
go
insert into #test
select '1' ,'2020-07-07 08:08:08','2' union all
select '1' ,'2020-07-07 08:08:08','1' union all
select '1' ,'2020-07-07 08:09:08','1' union all
select '1' ,'2020-07-07 08:10:08','1' union all
select '1' ,'2020-07-07 08:11:08','1' union all
select '2' ,'2020-07-07 08:08:08','1' union all
select '2' ,'2020-07-07 08:11:08','1' union all
select '2' ,'2020-07-07 08:12:08','1'union all
select '2' ,'2020-07-07 08:13:08','1'
select * from #test
;with c_1 as (select
ROW_NUMBER()over(partition by pin,reader_state order by event_time)as Num,
pin,event_time,reader_state
from #test group by pin,event_time,reader_state),
c_2 as(select a.Pin,a.event_time as STime,b.event_time as ETime,DATEDIFF(MINUTE,a.event_time,b.event_time)as jg,a.reader_state,a.Num as lNum,b.Num as rNum from c_1 as a left join c_1 as b on a.Num=b.Num-1 and a.pin=b.pin and a.reader_state=b.reader_state and DATEDIFF(MINUTE,a.event_time,b.event_time)<3 where a.Num=1
union all
select a.Pin,a.event_time as STime,b.event_time as ETime,DATEDIFF(MINUTE,a.event_time,b.event_time)as jg,a.reader_state,a.Num as lNum,b.Num as rNum from c_2 as c join c_1 as a on a.Num=ISNULL(c.rNum,c.lNum)+1 and a.pin=c.pin and a.reader_state=c.reader_state join c_1 as b on a.Num=b.Num-1 and a.pin=b.pin and a.reader_state=b.reader_state and DATEDIFF(MINUTE,a.event_time,b.event_time)<3
)
select a.pin,a.STime,a.ETime ,a.jg,a.reader_state from c_2 as a
union all
select a.pin,a.event_time as STime,null as ETime ,null as jg,a.reader_state from c_1 as a where a.Num>1 and not exists(select 1 from c_1 b where a.pin=b.pin and a.Num=b.Num-1 and a.reader_state=b.reader_state and DATEDIFF(MINUTE,a.event_time,b.event_time)<3)
order by Pin,STime
--result
/*
1 2020-07-07 08:08:08.000 2020-07-07 08:09:08.000 1 1
1 2020-07-07 08:08:08.000 NULL NULL 2
1 2020-07-07 08:10:08.000 2020-07-07 08:11:08.000 1 1
1 2020-07-07 08:11:08.000 NULL NULL 1
2 2020-07-07 08:08:08.000 NULL NULL 1
2 2020-07-07 08:11:08.000 2020-07-07 08:12:08.000 1 1
2 2020-07-07 08:13:08.000 NULL NULL 1
*/