select twoqudao as 名称,count(Invoice)as 单数1 from Customer_Service  
where twoqudao like '%%' and
twoqudao<>'汇展' and
twoqudao<>'非训' and
twoqudao<>'泥煤' and
sjfddate is null  and
kehuqianshoudate is null or
kehuqianshoudate=''  
group by twoqudao
--得出如下结果
/*姓名 单数1
张三 2
李思 2
王二 1
*/
select twoqudao as 名称,count(Invoice)as 单数2 from Customer_Service
where twoqudao like '%%' and
twoqudao<>'汇展' and
twoqudao<>'非训' and 
twoqudao<>'泥煤' and 
sjfddate is null or 
sjfddate='' 
group by twoqudao
--得出如下结果
/*姓名 单数2
张三 3
李思 2
王二 8
三五 9
*/--最终想要的结果
/*姓名 单数1 单数2
张三 2 3
李思 2 2
王二 1 8
三五 Null 9
*/

解决方案 »

  1.   


    select twoqudao as 名称,count(Invoice)as 单数1 from Customer_Service  
    where twoqudao like '%%' and
    twoqudao<>'汇展' and
    twoqudao<>'非训' and
    twoqudao<>'泥煤' and
    sjfddate is null  and
    (isnull(kehuqianshoudate,'')='' or isnull(sjfddate,'')='')
    group by twoqudao
      

  2.   


    --应是这样子
    select twoqudao as 名称,
    count(case when isnull(kehuqianshoudate,'')='' then Invoice else null end)as 单数1 
    count(case when isnull(sjfddate,'')='' then Invoice else null end)as 单数2
    from Customer_Service  
    where twoqudao like '%%' and
    twoqudao<>'汇展' and
    twoqudao<>'非训' and
    twoqudao<>'泥煤' and
    sjfddate is null  and
    (isnull(kehuqianshoudate,'')='' or isnull(sjfddate,'')='')
    group by twoqudao
      

  3.   


    select twoqudao as 名称,
    count(case when isnull(kehuqianshoudate,'')='' then Invoice else null end)as 单数1 
    count(case when isnull(sjfddate,'')='' then Invoice else null end)as 单数2
    from Customer_Service  
    where twoqudao like '%%' and
    twoqudao<>'汇展' and
    twoqudao<>'非训' and
    twoqudao<>'泥煤' and
    --sjfddate is null  and去掉,呵呵
    (isnull(kehuqianshoudate,'')='' or isnull(sjfddate,'')='')
    group by twoqudao
      

  4.   

    直接两条语句left join 不就可以了?
      

  5.   

    select
      a.*,b.单数2
    from
    (
    select twoqudao as 名称,count(Invoice)as 单数1 from Customer_Service  
    where twoqudao like '%%' and
    twoqudao<>'汇展' and
    twoqudao<>'非训' and
    twoqudao<>'泥煤' and
    sjfddate is null  and
    kehuqianshoudate is null or
    kehuqianshoudate=''  
    group by twoqudao
    )a
    left join
    (
    select twoqudao as 名称,count(Invoice)as 单数2 from Customer_Service
    where twoqudao like '%%' and
    twoqudao<>'汇展' and
    twoqudao<>'非训' and 
    twoqudao<>'泥煤' and 
    sjfddate is null or 
    sjfddate='' 
    group by twoqudao
    )b
    on
     a.名称=b.名称
      

  6.   

    select
      a.*,b.单数2
    from
    (
    select twoqudao as 名称,count(Invoice)as 单数1 from Customer_Service  
    where twoqudao like '%%' and
    twoqudao<>'汇展' and
    twoqudao<>'非训' and
    twoqudao<>'泥煤' and
    sjfddate is null  and
    kehuqianshoudate is null or
    kehuqianshoudate=''  
    group by twoqudao
    )a
    left join
    (
    select twoqudao as 名称,count(Invoice)as 单数2 from Customer_Service
    where twoqudao like '%%' and
    twoqudao<>'汇展' and
    twoqudao<>'非训' and 
    twoqudao<>'泥煤' and 
    sjfddate is null or 
    sjfddate='' 
    group by twoqudao
    )b
    on
     a.名称=b.名称