select ENTERPRISE_NUM ,
        SUM(CASE WHEN status = 'A'  then 1 else 0 end) , 
        SUM(CASE WHEN status = 'Z'  then 1 else 0 end) 
from  virtel_telrecord  
where start_time>= '2006-8-2  10:35:12'
     and sn not in
             (select b.sn from virtel_telrecord b ,virtel_telrecord c
              where b.source = c.source and b.dest = c.dest  
                   and b.sn !=c.sn and b.status = 'Z'
                   and  c.end_time = b.start_time)
group by ENTERPRISE_NUM 帮帮忙

解决方案 »

  1.   

    -- tryselect a.ENTERPRISE_NUM ,
    SUM(CASE WHEN a.status = 'A' then 1 else 0 end) ,
    SUM(CASE WHEN a.status = 'Z' then 1 else 0 end)
    from
    virtel_telrecord a
    left join
    (select b.sn from virtel_telrecord b ,virtel_telrecord c
    where b.source = c.source and b.dest = c.dest
    and b.sn !=c.sn and b.status = 'Z'
    and c.end_time = b.start_time) b
    on a.sn = b.sn
    where a.start_time >= '2006-8-2 10:35:12' and b.sn is null
    group by a.ENTERPRISE_NUM