SQL Server 使用union all查詢多個條件數據合並分組顯示,同比統計


    select CONVERT(char(7),a.created_yearmonth,20) created_yearmonth,
        a.countaccount countaccount,
        a.yxsl yxsl,
        a.sccdsl sccdsl,
        a.zccdsl zccdsl  
    from 
    (--總數
    select         CONVERT(char(7),account.created,20) created_yearmonth,
        count(1) countaccount,
        null yxsl,
        null sccdsl,
        null zccdsl  
    from account account
    left join org_dep iddep 
    on iddep.id=account.iddep
    left join org_employee idowner 
    on idowner.id=account.idowner 
    group by        CONVERT(char(7),account.created,20)        
        union all
        --有效    
    select         CONVERT(char(7),account.created,20) created_yearmonth,
        null countaccount,
        count(1)  yxsl,
        null sccdsl,
        null zccdsl  
    from account account
    left join org_dep iddep 
    on iddep.id=account.iddep
    left join org_employee idowner 
    on idowner.id=account.idowner 
    where account.emshzt in( 'bad4d977a06604e2ec5621bd0285eef2') 
    group by        CONVERT(char(7),account.created,20)
        union all
        --首次成交    
    select         CONVERT(char(7),account.created,20) created_yearmonth,
        null countaccount,
        null yxsl,
        count(1) sccdsl,
        null zccdsl  
    from account account
    left join org_dep iddep 
    on iddep.id=account.iddep
    left join org_employee idowner 
    on idowner.id=account.idowner 
    where account.dbcdcs = 1
    group by        CONVERT(char(7),account.created,20)
        union all
        --再次成交    
    select         CONVERT(char(7),account.created,20) created_yearmonth,
        null countaccount,
        null yxsl,
        null sccdsl,
        count(1) zccdsl  
    from account account
    left join org_dep iddep 
    on iddep.id=account.iddep
    left join org_employee idowner 
    on idowner.id=account.idowner 
    where account.dbcdcs > 1
    group by        CONVERT(char(7),account.created,20)
    ) a
     

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM