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

  • 相关阅读:
    找上海或深圳塑胶模具工作
    词典新选择网易推出有道[注意第二幅图中的红框]
    《天气与生活》修正版已发布, 以后将由南海维护
    JQuery发布1.2.3
    网游产业的公关
    使用ExtJS,失去的将比得到的还多
    养成习惯,解放大脑
    Vancl网上购衫体验
    深圳人均GDP过一万美元随想
    DateChooser ASP.NET 2.0版 之实用版
  • 原文地址:https://www.cnblogs.com/passerlee/p/11418940.html
Copyright © 2011-2022 走看看