招行理財金額頻次分段SQL記錄

2021-10-01 10:59:35 字數 2144 閱讀 9020

# 招行理財、金額分段、頻次分段

select

amount_num as "金額分段",

cishu as "頻次分段",

sum(total_amount) as "總交易額",

sum(total_chengben) as "總成本",

sum(meiren_bishu) as "總筆數",

count(f_trans_buyer_user_id) as "總人數"

from

( select

amount_num,

f_trans_buyer_user_id,

count(f_trans_buyer_user_id) as meiren_bishu,

sum(f_trans_amount) as total_amount,

sum(f_business_cost_amount) as total_chengben,

case when count(f_trans_buyer_user_id) = 1 then '1次'

when count(f_trans_buyer_user_id) = 2 then '2次'

when count(f_trans_buyer_user_id) = 3 then '3次'

when count(f_trans_buyer_user_id) = 4 then '4次'

when count(f_trans_buyer_user_id) = 5 then '5次'

when count(f_trans_buyer_user_id) > 5 then '5次+'

end as cishu

from

(select

f_trans_buyer_user_id,

f_trans_amount,

f_business_cost_amount,

case when f_trans_amount >= 0 and f_trans_amount <= 100000 then '1區間 0~1000'

when f_trans_amount > 100000 and f_trans_amount <= 500000 then '2區間 1001~5000'

when f_trans_amount > 500000 and f_trans_amount <= 1000000 then '3區間 5001~10000'

when f_trans_amount > 1000000 and f_trans_amount <= 3000000 then '4區間 10001~30000'

when f_trans_amount > 3000000 and f_trans_amount <= 5000000 then '5區間 3w~5w'

when f_trans_amount > 5000000 and f_trans_amount <= 10000000 then '6區間 5w~10w'

when f_trans_amount > 10000000 and f_trans_amount <= 50000000 then '7區間 10w~50w'

when f_trans_amount > 50000000 and f_trans_amount <= 100000000 then '8區間 50w~100w'

when f_trans_amount > 100000000 then '9區間 100w+'

end as amount_num

from t_wallet_info

where f_recvable_recv_bank_id

in (招行前端編碼)

and f_trans_seller_user_id

in (理財場景商戶uid)

and f_business_time >= '2019-01-01 00:00:00'

and f_business_time <= '2019-07-30 23:59:59'

and f_business_type in (5,7,8)

) agroup by amount_num,f_trans_buyer_user_id

) bgroup by amount_num,cishu

order by amount_num,cishu;