SQL话语查询,同表group by 提取有两个相同字段的数据
SQL语句查询,同表group by 提取有两个相同字段的数据
表结构tb_submit(有主键,keyid)
custid submit date
1 err 2013/5/1 11:00:01
1 ok 2013/5/1 11:00:03
1 err 2013/5/1 11:00:02
2 err 2013/5/1 12:00:01
2 ok 2013/5/1 11:00:03
2 err 2013/5/1 11:00:02
3 err 2013/5/1 12:00:01
3 ok 2013/5/1 11:00:03
3 ok 2013/5/1 11:00:02
现在想要提取tb_submit其中的数据。
同个custid下 有两条submit为err状态,且两个err状态的时间相差10秒内,跪求代码!
比如上面的例子,就会提取出
custid submit date
1 err 2013/5/1 11:00:01
1 err 2013/5/1 11:00:02
2 err 2013/5/1 12:00:01
2 err 2013/5/1 11:00:02
谢谢各位大牛!!!。
------解决方案--------------------
------解决方案--------------------
表结构tb_submit(有主键,keyid)
custid submit date
1 err 2013/5/1 11:00:01
1 ok 2013/5/1 11:00:03
1 err 2013/5/1 11:00:02
2 err 2013/5/1 12:00:01
2 ok 2013/5/1 11:00:03
2 err 2013/5/1 11:00:02
3 err 2013/5/1 12:00:01
3 ok 2013/5/1 11:00:03
3 ok 2013/5/1 11:00:02
现在想要提取tb_submit其中的数据。
同个custid下 有两条submit为err状态,且两个err状态的时间相差10秒内,跪求代码!
比如上面的例子,就会提取出
custid submit date
1 err 2013/5/1 11:00:01
1 err 2013/5/1 11:00:02
2 err 2013/5/1 12:00:01
2 err 2013/5/1 11:00:02
谢谢各位大牛!!!。
------解决方案--------------------
with t1 as
(select t.custid,
(t.day - lag(t.day) over(partition by t.custid order by day)) * 24 * 60 * 60 lday,
count(1) over(partition by custid) cnt
from tb_submit t
where t.submit = 'err')
select *
from tb_submit t
where exists (select 1
from t1
where t.custid = t1.custid
and t1.lday <= 10
and t1.cnt = 2)
and t.submit = 'err';
------解决方案--------------------
with t1 as
(
select 1 keyid,1 custid,'err' submit,to_date('2013-05-01 11:00:01','yyyy-mm-dd hh24:mi:ss') tdate from dual union all
select 2 keyid,1 custid,'ok' submit,to_date('2013-05-01 11:00:03','yyyy-mm-dd hh24:mi:ss') tdate from dual union all
select 3 keyid,1 custid,'err' submit,to_date('2013-05-01 11:00:04','yyyy-mm-dd hh24:mi:ss') tdate from dual union all
select 4 keyid,2 custid,'err' submit,to_date('2013-05-01 11:00:01','yyyy-mm-dd hh24:mi:ss') tdate from dual union all
select 5 keyid,2 custid,'ok' submit,to_date('2013-05-01 11:00:03','yyyy-mm-dd hh24:mi:ss') tdate from dual union all
select 6 keyid,2 custid,'err' submit,to_date('2013-05-01 11:00:02','yyyy-mm-dd hh24:mi:ss') tdate from dual union all
select 7 keyid,3 custid,'err' submit,to_date('2013-05-01 11:00:01','yyyy-mm-dd hh24:mi:ss') tdate from dual union all
select 8 keyid,3 custid,'ok' submit,to_date('2013-05-01 11:00:03','yyyy-mm-dd hh24:mi:ss') tdate from dual union all