zoukankan      html  css  js  c++  java
  • case when 嵌套,行专列

    case  when Expendunit is null then (case when Expendunit1 is null then (case when Expendunit2 is null then (case when Expendunit3 is null then Expendunit4 else Expendunit3 end) else Expendunit2 end) else Expendunit1 end) else Expendunit end Expendunit

    select case  when Expendunit is null then (case when Expendunit1 is null then (case when Expendunit2 is null then (case when Expendunit3 is null then Expendunit4 else Expendunit3 end) else Expendunit2 end) else Expendunit1 end) else Expendunit end Expendunit,aa.rmb,bb.rmb1,cc.rmb2,dd.rmb3,ee.rmb4 from (select top 100000 Expendunit,sum(RMB) rmb,case Settletype when 0 then '电汇'  when 1 then '承兑' when 2 then '现金' when 3 then '手机支付' when 4 then '转账'  end Settletype from Expenditure where businessdate >='2019-08-01 00:00:00.000' and businessdate<='2019-08-31 00:00:00.000' and settletype=2 group by  Expendunit,Settletype order by Expendunit,Settletype) aa
    full outer join (select top 100000 Expendunit Expendunit1,sum(RMB) rmb1,case Settletype when 0 then '电汇'  when 1 then '承兑' when 2 then '现金' when 3 then '手机支付' when 4 then '转账'  end Settletype from Expenditure where businessdate >='2019-08-01 00:00:00.000' and businessdate<='2019-08-31 00:00:00.000' and settletype=0 group by  Expendunit,Settletype order by Expendunit1,Settletype) bb on aa.Expendunit=bb.Expendunit1
    full outer join (select top 100000 Expendunit Expendunit2,sum(RMB) rmb2,case Settletype when 0 then '电汇'  when 1 then '承兑' when 2 then '现金' when 3 then '手机支付' when 4 then '转账'  end Settletype from Expenditure where businessdate >='2019-08-01 00:00:00.000' and businessdate<='2019-08-31 00:00:00.000' and settletype=1 group by  Expendunit,Settletype order by Expendunit2,Settletype) cc on bb.Expendunit1=cc.Expendunit2
    full outer join(select top 100000 Expendunit Expendunit3,sum(RMB) rmb3,case Settletype when 0 then '电汇'  when 1 then '承兑' when 2 then '现金' when 3 then '手机支付' when 4 then '转账'  end Settletype from Expenditure where businessdate >='2019-08-01 00:00:00.000' and businessdate<='2019-08-31 00:00:00.000' and settletype=3 group by  Expendunit,Settletype order by Expendunit3,Settletype) dd on cc.Expendunit2=dd.Expendunit3
    full outer join(select top 100000 Expendunit Expendunit4,sum(RMB) rmb4,case Settletype when 0 then '电汇'  when 1 then '承兑' when 2 then '现金' when 3 then '手机支付' when 4 then '转账'  end Settletype from Expenditure where businessdate >='2019-08-01 00:00:00.000' and businessdate<='2019-08-31 00:00:00.000' and settletype=4 group by  Expendunit,Settletype order by Expendunit4,Settletype) ee on dd.Expendunit3=ee.Expendunit4
    union all
    select '合计' Expendunit,sum(rmb) rmb,sum(rmb1) rmb1,sum(rmb2) rmb2,sum(rmb3) rmb3,sum(rmb4) rmb4 from (select case  when Expendunit is null then (case when Expendunit1 is null then (case when Expendunit2 is null then (case when Expendunit3 is null then Expendunit4 else Expendunit3 end) else Expendunit2 end) else Expendunit1 end) else Expendunit end Expendunit,aa.rmb,bb.rmb1,cc.rmb2,dd.rmb3,ee.rmb4 from (select top 100000 Expendunit,sum(RMB) rmb,case Settletype when 0 then '电汇'  when 1 then '承兑' when 2 then '现金' when 3 then '手机支付' when 4 then '转账'  end Settletype from Expenditure where businessdate >='2019-08-01 00:00:00.000' and businessdate<='2019-08-31 00:00:00.000' and settletype=2 group by  Expendunit,Settletype order by Expendunit,Settletype) aa
    full outer join (select top 100000 Expendunit Expendunit1,sum(RMB) rmb1,case Settletype when 0 then '电汇'  when 1 then '承兑' when 2 then '现金' when 3 then '手机支付' when 4 then '转账'  end Settletype from Expenditure where businessdate >='2019-08-01 00:00:00.000' and businessdate<='2019-08-31 00:00:00.000' and settletype=0 group by  Expendunit,Settletype order by Expendunit1,Settletype) bb on aa.Expendunit=bb.Expendunit1
    full outer join (select top 100000 Expendunit Expendunit2,sum(RMB) rmb2,case Settletype when 0 then '电汇'  when 1 then '承兑' when 2 then '现金' when 3 then '手机支付' when 4 then '转账'  end Settletype from Expenditure where businessdate >='2019-08-01 00:00:00.000' and businessdate<='2019-08-31 00:00:00.000' and settletype=1 group by  Expendunit,Settletype order by Expendunit2,Settletype) cc on bb.Expendunit1=cc.Expendunit2
    full outer join(select top 100000 Expendunit Expendunit3,sum(RMB) rmb3,case Settletype when 0 then '电汇'  when 1 then '承兑' when 2 then '现金' when 3 then '手机支付' when 4 then '转账'  end Settletype from Expenditure where businessdate >='2019-08-01 00:00:00.000' and businessdate<='2019-08-31 00:00:00.000' and settletype=3 group by  Expendunit,Settletype order by Expendunit3,Settletype) dd on cc.Expendunit2=dd.Expendunit3
    full outer join(select top 100000 Expendunit Expendunit4,sum(RMB) rmb4,case Settletype when 0 then '电汇'  when 1 then '承兑' when 2 then '现金' when 3 then '手机支付' when 4 then '转账'  end Settletype from Expenditure where businessdate >='2019-08-01 00:00:00.000' and businessdate<='2019-08-31 00:00:00.000' and settletype=4 group by  Expendunit,Settletype order by Expendunit4,Settletype) ee on dd.Expendunit3=ee.Expendunit4) ff

  • 相关阅读:
    科学家质疑当今商用量子计算机的性能
    科学家研制出可模拟大脑信息处理的微芯片
    2014年电子科技市场衰退
    号外!CentOS 宣布加入红帽公司!
    hadoop,高富帅的玩具?
    成为Linux内核高手的四个方法
    分阶段事件驱动架构【SEDA】
    原型程式设计【原型语言】
    IOS7.1 企业应用 证书无效 已解决
    iOS7.1企业应用"无法安装应用程序 因为证书无效"的解决方案
  • 原文地址:https://www.cnblogs.com/passerlee/p/11418940.html
Copyright © 2011-2022 走看看