日期:2014-05-17  浏览次数:20770 次

oracle 按要求 查询数据,看上简单.... 希望有高人可以做到啊。
表信息

连锁店编号 会员卡号 录入员工号 录入终端 录入时间  
2012001001 12345678 2012000001 00000001 20120510 16:29:23
2012001001 12345678 2012000001 00000001 20120510 16:30:44
2012001001 12345678 2012000001 00000001 20120510 16:39:12
2012001004 12345678 2012000002 00000001 20120510 16:41:21
2012001004 32345678 2012000002 00000002 20120510 16:43:56
2012001004 22345678 2012000002 00000001 20120510 16:46:49
2012001004 52345678 2012000002 00000002 20120510 16:47:23
2012001001 32345678 2012000003 00000001 20120510 16:49:10
2012001002 82345678 2012000003 00000002 20120510 16:53:08
2012001004 92345678 2012000003 00000003 20120510 16:57:00
2012001005 92345678 2012000003 00000002 20120510 17:01:51
2012001003 82345678 2012000003 00000003 20120510 17:02:23
查询
同一连锁店当日同一会员卡号,在相同的终端连续2次以上(含2次)录入时间=<2分钟

------解决方案--------------------
SQL code
with t as (
select 
'2012001001' a,'12345678' b,'2012000001' c,'1' d,to_date('20120510162923','yyyymmddhh24miss') e from dual union all select 
'2012001001','12345678','2012000001','1',to_date('20120510163044','yyyymmddhh24miss') from dual union all select 
'2012001001','12345678','2012000001','1',to_date('20120510163912','yyyymmddhh24miss')from dual union all select 
'2012001004','12345678','2012000002','1',to_date('20120510164121','yyyymmddhh24miss')from dual union all select 
'2012001004','32345678','2012000002','2',to_date('20120510164356','yyyymmddhh24miss')from dual union all select 
'2012001004','22345678','2012000002','1',to_date('20120510164649','yyyymmddhh24miss')from dual union all select 
'2012001004','52345678','2012000002','2',to_date('20120510164723','yyyymmddhh24miss')from dual union all select 
'2012001001','32345678','2012000003','1',to_date('20120510164910','yyyymmddhh24miss')from dual union all select 
'2012001002','82345678','2012000003','2',to_date('20120510165308','yyyymmddhh24miss')from dual union all select 
'2012001004','92345678','2012000003','3',to_date('20120510165700','yyyymmddhh24miss')from dual union all select 
'2012001005','92345678','2012000003','2',to_date('20120510170151','yyyymmddhh24miss')from dual union all select 
'2012001003','82345678','2012000003','3',to_date('20120510170223','yyyymmddhh24miss') from dual

)

select t.* from t where exists(
select 1 from t a where t.a=a.a and t.b=a.b and t.d= a.d
and abs(t.e-a.e)<=2/(24*60) and abs(t.e-a.e)>0
)

---------------------------------
2012001001    12345678    2012000001    1    2012-5-10 16:29:23
2012001001    12345678    2012000001    1    2012-5-10 16:30:44

------解决方案--------------------
SQL code

--这样求出来的是连续两次输入时间的间隔<=2分钟,不知道是不是楼主的要求

select distinct 连锁店编号, 会员卡号, 录入员工号, 录入终端, 录入时间
from tab t1
where exists 
(select 1 from tab t2
where t1.连锁店编号 = t2.连锁店编号 and
t1.会员卡号 = t2.会员卡号 and
t1.录入员工号 = t2.录入员工号 and
t1.录入终端 = t2.录入终端 and
t1.录入时间 <> t2.录入时间 and
(t1.录入时间 - t2.录入时间)*24*60 >=-2 and
(t1.录入时间 - t2.录入时间)*24*60 <=2