一表:
 Create Table test19(a VARCHAR2(2),code INTEGER);
数据:
 Insert Into test19 VALUES('A',1);
 Insert Into test19 VALUES('A',2);
 Insert Into test19 VALUES('A',3); 
 Insert Into test19 VALUES('A',4);  
 Insert Into test19 VALUES('B',1);
 Insert Into test19 VALUES('B',2);
 Insert Into test19 VALUES('B',3); 
 Insert Into test19 VALUES('B',4);   
 Insert Into test19 VALUES('C',1);
 Insert Into test19 VALUES('C',2);
 Insert Into test19 VALUES('C',3);  
 Insert Into test19 VALUES('C',4);    
 Insert Into test19 VALUES('D',1);
 Insert Into test19 VALUES('D',4);
 Insert Into test19 VALUES('E',1);   
 Insert Into test19 VALUES('E',4);
----------------
code 1,为投入站,3为产出站(如果没有3的,4为产出站);
结果:
**,inall,outall
A   1    ,1
B   1    ,1
C   1    ,1
D   1    ,1
E   1    ,1
有没有看明白?

解决方案 »

  1.   

    select a.a,a.inall,b.outall
    from tb a
    left join (select a,',1' outall from tb where inall='3' or inall='4')b
    on a.a=b.a
    where a.inall='1';
      

  2.   

    select a,
     sum(decode(code,1,1,0)) as inall,
     decode(sum(decode(code,4,1,3,1,0)),0,0,1) as outall,
    from test19 group by a;
      

  3.   

    select a, inall, decode(outall3,0,outall4,outall3) outall from
    (
    select a, sum(decode(code,1,1,0)) inall, sum(decode(code,3,1,0)) outall3, sum(decode(code,4,1,0)) outall4
    from test19
    group by a
    order by a);
      

  4.   


    select a,
     sum(decode(code,1,1,0)) as inall,
     decode(sum(decode(code,4,1,3,1,0)),0,0,1) as outall,
    from test19 
    group by a;