select n1,n2,n3,count(*) from 
(
select 
       (select d.name from table2 d where a.type1=d.myid) n1,
       (select d.name from table2 d where a.type2=d.myid) n2,
       (select d.name from table2 d where a.type3=d.myid) n3 from table1 a  
where not exists(
select 1 from table2 b
where a.type1 is not null and a.type1 = b.myid and b.sysid <> '1'
) /* 排除table1中非主系统的记录 */
) z
group by n1,n2,n3
order by n1,n2,n3

解决方案 »

  1.   

    select d.type1,d.type2,d.type3,d.count
    from
    ( select 
    b1.name type1,b2.name type2,b3.name type3,count(*) count
    from 
    table1 a,
    (select myid,name,depth,sysid,parentid from table2 where depth='0') b1,
    (select myid,name,depth,sysid,parentid from table2 where depth='1') b2,
    (select myid,name,depth,sysid,parentid from table2 where depth='2') b3
    where 
    a.type1=b1.myid(+) and
    a.type2=b2.myid(+) and
    a.type3=b3.myid(+) and
    b1.sysid(+)='1' and
    b2.sysid(+)='1' and
    b3.sysid(+)='1'
    group by 
    b1.name,b2.name,b3.name
    ) d
    where 
    d.type1 is not null or 
    d.type2 is not null or 
    d.type3 is not null