case when 簡單應用

select
    qujian,
    count(*) as total_cnt,
    sum(F_trans_amount) as total_amount,
    count(case when F_recvable_bank_user_id=8998 then F_recvable_bank_user_id end) as jhxd_cnt,
    sum(case when F_recvable_bank_user_id=8998 then F_trans_amount end) as jhxd_amount
from
(
    select
        F_recvable_bank_user_id,
        F_trans_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 < 200000 then '2區間 1000-2000'
        when F_trans_amount >= 200000 and F_trans_amount < 300000 then '3區間 2000-3000'
        when F_trans_amount >= 300000 and F_trans_amount < 400000 then '4區間 3000-4000'
        when F_trans_amount >= 400000 and F_trans_amount < 500000 then '5區間 4000-5000'
        when F_trans_amount >= 500000 and F_trans_amount < 1000000 then '6區間 5000-10000'
        when F_trans_amount >= 1000000 and F_trans_amount < 1500000 then '7區間 1w-1.5w'
        when F_trans_amount >= 1500000 then '8區間 1.5w+'
        end as qujian
    from wallet.t_wallet_info
    where F_trans_type <> 17
    AND F_recvable_bank_user_id != 202
    and F_trans_seller_user_id
    in (select f_sp_user_id from fn.t_scenario_sp_map where F_sp_scenario in (40000,40001,40002))
    and F_business_time >= '2020-04-01 00:00:00'
    AND F_business_time <= '2020-04-30 23:59:59'
    AND F_business_type in (5,6,7)
) t1
group by qujian
order by qujian;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章