SQL横向添加

select t.customer, count(t.customer)
  from tablea t
 where t.phoneno in ('777777', '222222')
 group by t.customer order by t.customer;
 
 
 select t.customer, count(t.customer)
  from tablea t
 where t.phoneno = '777777'
 group by t.customer order by t.customer;
 
 select t.customer, count(t.customer)
  from tablea t
 where t.phoneno = '222222'
 group by t.customer order by t.customer;
 
 
 
select C.*,nvl(D.count_2,0)
  from (select A.customer, A.count, B.count_7
          from (select t.customer, count(t.customer) as count
                  from tablea t
                 where t.phoneno in ('777777', '222222')
                 group by t.customer
                 order by t.customer) A
          left join (select t.customer, count(t.customer) as count_7
                      from tablea t
                     where t.phoneno = '777777'
                     group by t.customer
                     order by t.customer) B on A.customer = B.customer) C
  left join (select t.customer, count(t.customer) as count_2
               from tablea t
              where t.phoneno = '222222'
              group by t.customer
              order by t.customer) D on C.customer = D.customer

猜你喜欢

转载自zyssnh.iteye.com/blog/1501711