有如下的原始数据,我需要统计出连续时间段内, 同一LINE, PN , Station , Status= 'FAIL'时,同一个ERRCODE出现3次以上的数据
LINE | SN | PN | Station | Status | ERRCODE | DT |
LINE1 | SN1 | PN1 | Station1 | PASS | 2012-03-21 09:01:01 | |
LINE1 | SN2 | PN2 | Station1 | PASS | 2012-03-21 09:01:02 | |
LINE1 | SN3 | PN2 | Station1 | FAIL | F1 | 2012-03-21 09:01:03 |
LINE1 | SN4 | PN2 | Station1 | FAIL | F1 | 2012-03-21 09:01:04 |
LINE1 | SN5 | PN2 | Station1 | FAIL | F1 | 2012-03-21 09:01:05 |
LINE2 | SN6 | PN2 | Station2 | PASS | 2012-03-21 09:01:06 | |
LINE2 | SN7 | PN2 | Station2 | PASS | 2012-03-21 09:01:07 | |
LINE1 | SN8 | PN2 | Station1 | PASS | 2012-03-21 09:01:08 | |
LINE2 | SN9 | PN1 | Station2 | PASS | 2012-03-21 09:01:09 | |
LINE1 | SN10 | PN1 | Station2 | PASS | 2012-03-21 09:01:10 | |
LINE1 | SN11 | PN1 | Station2 | FAIL | F2 | 2012-03-21 09:01:11 |
LINE3 | SN12 | PN1 | Station1 | FAIL | F2 | 2012-03-21 09:01:12 |
LINE1 | SN13 | PN1 | Station2 | FAIL | F2 | 2012-03-21 09:01:13 |
LINE3 | SN14 | PN1 | Station2 | FAIL | F2 | 2012-03-21 09:01:14 |
LINE1 | SN15 | PN1 | Station2 | PASS | 2012-03-21 09:01:15 | |
LINE1 | SN16 | PN2 | Station1 | PASS | 2012-03-21 09:01:16 | |
LINE3 | SN17 | PN2 | Station1 | PASS | 2012-03-21 09:01:17 | |
LINE1 | SN18 | PN2 | Station1 | PASS | 2012-03-21 09:01:18 | |
LINE1 | SN19 | PN2 | Station1 | PASS | 2012-03-21 09:01:19 |
得到
LINE Station PN ERRCODE QTY
LINE1 Staion1 PN1 F1 3
请各位提示下
按LINE,SN,PN,Station,Status,ERRCODE,DT分组
不能按LINE,SN,PN,Station,Status,ERRCODE,DT分组,如果直接按这样的条件分组,永远无法统计出结果
@Bay.Li: select * from (select LINE,SN,PN,Station,Status,ERRCODE,DT,COUNT(LINE) AS CNT FROM T1 GROUP BY LINE,SN,PN,Station,Status,ERRCODE,to_char(SYSDATE, 'YYYY-MM-DD HH24:MI')) A WHERE A.CNT>=3
select line,station,pn,errcode,count(1) as qty from tb where status='fail' group by line,pn,station,errcode having count(1)>=3
create table tmp_20130304_t1(line varchar(50),sn varchar(50),pn varchar(50),station varchar(50),status varchar(50),errcode varchar(50),dt datetime) insert into tmp_20130304_t1 values('LINE1','SN1','PN1','Station1','PASS',' ','2012-03-21 09:01:01') insert into tmp_20130304_t1 values('LINE1','SN2','PN2','Station1','PASS',' ','2012-03-21 09:01:02') insert into tmp_20130304_t1 values('LINE1','SN3','PN2','Station1','FAIL','F1','2012-03-21 09:01:03') insert into tmp_20130304_t1 values('LINE1','SN4','PN2','Station1','FAIL','F1','2012-03-21 09:01:04') insert into tmp_20130304_t1 values('LINE1','SN5','PN2','Station1','FAIL','F1','2012-03-21 09:01:05') insert into tmp_20130304_t1 values('LINE2','SN6','PN2','Station2','PASS',' ','2012-03-21 09:01:06') insert into tmp_20130304_t1 values('LINE2','SN7','PN2','Station2','PASS',' ','2012-03-21 09:01:07') insert into tmp_20130304_t1 values('LINE1','SN8','PN2','Station1','PASS',' ','2012-03-21 09:01:08') insert into tmp_20130304_t1 values('LINE2','SN9','PN1','Station2','PASS',' ','2012-03-21 09:01:09') insert into tmp_20130304_t1 values('LINE1','SN10','PN1','Station2','PASS',' ','2012-03-21 09:01:10') insert into tmp_20130304_t1 values('LINE1','SN11','PN1','Station2','FAIL','F2','2012-03-21 09:01:11') insert into tmp_20130304_t1 values('LINE3','SN12','PN1','Station1','FAIL','F2','2012-03-21 09:01:12') insert into tmp_20130304_t1 values('LINE1','SN13','PN1','Station2','FAIL','F2','2012-03-21 09:01:13') insert into tmp_20130304_t1 values('LINE3','SN14','PN1','Station2','FAIL','F2','2012-03-21 09:01:14') insert into tmp_20130304_t1 values('LINE1','SN15','PN1','Station2','PASS',' ','2012-03-21 09:01:15') insert into tmp_20130304_t1 values('LINE1','SN16','PN2','Station1','PASS',' ','2012-03-21 09:01:16') insert into tmp_20130304_t1 values('LINE3','SN17','PN2','Station1','PASS',' ','2012-03-21 09:01:17') insert into tmp_20130304_t1 values('LINE1','SN18','PN2','Station1','PASS',' ','2012-03-21 09:01:18') insert into tmp_20130304_t1 values('LINE1','SN19','PN2','Station1','PASS',' ','2012-03-21 09:01:19') select * from tmp_20130304_t1 select line,station,pn,errcode,count(1) as qty from tmp_20130304_t1 where status='fail' group by line,pn,station,errcode having count(1)>=3
测试通过
@飞来飞去:
如果我在加上一条
insert into tmp_20130304_t1 values('LINE1','SN4','PN2','Station1','PASS','F1','2012-03-21 09:01:04:005')
得到
line station pn errcode qty
LINE1 Station1 PN2 F1 4
但是我的意思是要在连续的时间内,因为刚才我加的这一条Status=PASS破坏了连续性,因此统计结果为空才是正确的
@Bay.Li: 哦不好意思没注意 连续的时间 这个条件,那就复杂了
@飞来飞去: '
对你的支持深表感谢
你的这个就是一个孤岛问题
create view V_Tmp as select * from (select ID=ROW_NUMBER() OVER(order by dt),* from tmp_20130304_t1) a where status='fail' go select a.line,a.station,a.pn,a.errcode,count(1) as qty from V_Tmp A left join V_Tmp B on a.id+1 = b.id where A.line=B.line and A.pn=B.pn and A.station=B.station group by a.line,a.pn,a.station,a.errcode having count(1)>=2
谢谢