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
     

 

转载于:https://www.cnblogs.com/RainHouse/p/11137156.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值