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

posted @ 2019-08-27 16:07  一锤定音  阅读(800)  评论(0编辑  收藏  举报