SQL_Case When 嵌套

select 
psperdno "期数",
count(case when psperdno = 1 then (case when date(datadate)-psduedt > 0 then a.custid end) else (case when date(datadate)-psduedt > 15 then a.custid end) end) "总人数",
count(case when latedays > 0 and date(datadate)-psduedt > 0 then a.custid end) "d0逾期人数",
count(case when latedays > 0 and date(datadate)-psduedt > 0 then a.custid end)*1.0/count(case when psperdno = 1 then (case when date(datadate)-psduedt > 0 then a.custid end) else (case when date(datadate)-psduedt > 15 then a.custid end) end) "d0逾期率",
count(case when latedays > 15 and date(datadate)-psduedt > 15 then a.custid end) "d15逾期人数",
count(case when latedays > 15 and date(datadate)-psduedt > 15 then a.custid end)*1.0/count(case when psperdno = 1 then (case when date(datadate)-psduedt > 0 then a.custid end) else (case when date(datadate)-psduedt > 15 then a.custid end) end) "d15逾期率"
from 
transformdata.xsfinrank a
join
ylhx4.cxh_xinyanfeature b 
on a.custid=b.custid where brrank = 1  and psperdno in (1,2) group by 1;

参考

猜你喜欢

转载自blog.csdn.net/Datawhale/article/details/81781984
今日推荐