SQL中case when應用及Decimal類型數據的運算

-----------------------------------

理財計算

insert into lsyw.finance

select cust_num, cust_cn_nm, acct_belg_org, total, l1, 100*(cast(l1 as double)/total) as l1ra, m1l3, 100*(cast(m1l3 as double)/total) as m1l3ra,
m3l6, 100*(cast(m3l6 as double)/total) as m3l6ra, m6l12, 100*(cast(m6l12 as double)/total) as m6l12ra,
m12, 100*(cast(m12 as double)/total) as m12ra from
(
select a.cust_num, c.Cust_Cn_Nm, a.acct_belg_org, count(*) as total,
sum(case when (days(b.due_dt) - days(b.st_int_dt))/30 <= 1 then 1 else 0 end) as l1,
sum(case when (days(b.due_dt) - days(b.st_int_dt))/30 >1 and (days(b.due_dt) - days(b.st_int_dt))/30 <= 3 then 1 else 0 end) as m1l3,
sum(case when (days(b.due_dt) - days(b.st_int_dt))/30 >3 and (days(b.due_dt) - days(b.st_int_dt))/30 <= 6 then 1 else 0 end) as m3l6,
sum(case when (days(b.due_dt) - days(b.st_int_dt))/30 >6 and (days(b.due_dt) - days(b.st_int_dt))/30 <= 12 then 1 else 0 end) as m6l12,
sum(case when (days(b.due_dt) - days(b.st_int_dt))/30 >12 then 1 else 0 end) as m12
from edw.bhif_chrem_fund_sell_dtl a
left join edw.bhif_chrem_fund_prod b
on a.chrem_encd = b.prod_id
left join edw.bhif_indv_cust_basic_info c
on a.cust_num = c.cust_id
where year(b.st_int_dt) != 0001 and year(b.st_int_dt) != 2099 and year(b.due_dt) != 0001 and year(b.due_dt) != 2099 and year(b.due_dt) != 8999
group by cust_num, cust_cn_nm, acct_belg_org

)


-------------------------------------

工資計算

select 
stdgjjennm as 企業名稱, count(*) as 企業人數, sum(stdgjjdwam + stdgjjgram)/count(*) as 平均繳存金額, 
sum(stdgjjdwsc)/count(*) as 單位平均繳存比例,sum(stdgjjgrsc)/count(*) as 個人平均繳存比例, 
sum(stdgjjdwam/stdgjjdwsc)/count(*) as 平均工資,
sum(case when stdgjjdwam/stdgjjdwsc >= 0 and stdgjjdwam/stdgjjdwsc < 2000 then 1 else 0 end) as 工資0到2000元人數,
sum(case when stdgjjdwam/stdgjjdwsc >= 2000 and stdgjjdwam/stdgjjdwsc < 3000 then 1 else 0 end) as 工資2000到3000元人數,
sum(case when stdgjjdwam/stdgjjdwsc >= 3000 and stdgjjdwam/stdgjjdwsc < 4000 then 1 else 0 end) as 工資3000到4000元人數,
sum(case when stdgjjdwam/stdgjjdwsc >= 4000 and stdgjjdwam/stdgjjdwsc < 6000 then 1 else 0 end) as 工資4000到6000元人數,
sum(case when stdgjjdwam/stdgjjdwsc >= 6000 and stdgjjdwam/stdgjjdwsc < 8000 then 1 else 0 end) as 工資6000到8000元人數,
sum(case when stdgjjdwam/stdgjjdwsc >= 8000 and stdgjjdwam/stdgjjdwsc < 10000 then 1 else 0 end) as 工資8000到10000元人數,
sum(case when stdgjjdwam/stdgjjdwsc >= 10000 and stdgjjdwam/stdgjjdwsc < 15000 then 1 else 0 end) as 工資10000到15000元人數,
sum(case when stdgjjdwam/stdgjjdwsc >= 15000 and stdgjjdwam/stdgjjdwsc < 20000 then 1 else 0 end) as 工資15000到20000元人數,
sum(case when stdgjjdwam/stdgjjdwsc >= 20000 then 1 else 0 end) as 工資大於20000元人數

from gjj.gjjzh 

where stdgjjdwsc > 0

group by stdgjjennm



發佈了88 篇原創文章 · 獲贊 15 · 訪問量 34萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章