step1:使用prd_id相等条件将tb_clk_rcd左连接tb_prd_map;
step2:根据每件商品所属的范围增加新的范围列;
step3:限定条件if_buy=1;
step4:统计每个价格区间的人数;
step5:按人数降序排序。
Select price_cut, count(distinct cust_uid) as price_cut_num
FROM(
Select *,
CASE
WHEN price >= 0 and price < 50 THEN '0-50'
WHEN price >= 50 and price < 100 THEN '50-100'
WHEN price >= 100 and price <= 200 THEN '100-200'
END as price_cut
FROM(
SELECT a.prd_id, prd_nm,cust_uid,price, if_buy
From tb_clk_rcd a
left join tb_prd_map b
on a.prd_id=b.prd_id
)t
)tt
Where if_buy=1
Group by price_cut
ORDER BY price_cut_num desc;