• 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
         
  • 相关阅读:
    正方形_自适应_移动端
    meta name="viewport" content="width=device-width,initial-scale=1.0"
    :before/:after与::before/::after的区别 和属性content:值
    布局:flex弹性布局_兼容性写法
    布局:文本多列布局 column-* :
    布局:网格布局
    clear
    布局:盒模型 box-sizing : border-box ;
    object-fit : cover; 对象(图片和视频对象)
    布局:flex弹性布局_实践02
  • 原文地址:https://www.cnblogs.com/RainHouse/p/11137156.html
Copyright © 2020-2023  润新知