求占据前90%销售额的商品类型
472 字
2 分钟
求占据前90%销售额的商品类型
-- 求占据前90%销售额的商品类型-- step0. 准备数据create table cm.tb_sale_amount( good_category int comment '商品类型ID', sale_date date comment '销售日期', amount int comment '销售额', primary key(good_category, sale_date));
truncate cm.tb_sale_amount;
insert into cm.tb_sale_amount(good_category, sale_date, amount) values(1003, '2022-01-10', 264),(1001, '2022-06-01', 21),(1005, '2022-06-01', 73),(1002, '2022-06-27', 44),(1006, '2022-06-27', 405),(1003, '2022-09-10', 16),(1005, '2022-09-13', 72),(1004, '2022-10-01', 29),(1005, '2022-10-03', 332),(1001, '2022-10-29', 10),(1006, '2022-10-29', 137),(1002, '2022-12-02', 23),(1007, '2022-12-02', 19),(1003, '2022-12-02', 30),(1008, '2022-12-03', 3),(1009, '2022-12-04', 1),(1010, '2022-12-05', 9),(1003, '2022-12-30', 121);
-- step1. 计算每种商品的总销售额,并降序排序select good_category, sum(amount) as good_amountfrom cm.tb_sale_amountgroup by good_categoryorder by good_amount desc;
-- step2. 求全部商品的总销售额,为了step3求各种商品的占比,需要先求和。注意:求总和时,窗口值既不排序也不进行分组select *, sum(good_amount) over () as all_amountfrom (select good_category, sum(amount) as good_amount from cm.tb_sale_amount group by good_category order by good_amount desc ) t1;
-- step3. 求占比select *, good_amount * 1.0 / all_amount as ratiofrom (select *, sum(good_amount) over () as all_amount from (select good_category, sum(amount) as good_amount from cm.tb_sale_amount group by good_category order by good_amount desc ) as t1 ) t2;
-- step4. 求累计占比,注意:求累计值时,一定要进行排序select *, sum(ratio) over (order by ratio desc) as acc_ratiofrom( select *, good_amount * 1.0 / all_amount as ratio from (select *, sum(good_amount) over () as all_amount from (select good_category, sum(amount) as good_amount from cm.tb_sale_amount group by good_category order by good_amount desc ) as t1 ) t2 ) t3;
-- step5. 求前一行的累计占比select *, lag(acc_ratio) over() as pre_acc_ratiofrom(select *, sum(ratio) over (order by ratio desc) as acc_ratio from( select *, good_amount * 1.0 / all_amount as ratio from (select *, sum(good_amount) over () as all_amount from (select good_category, sum(amount) as good_amount from cm.tb_sale_amount group by good_category order by good_amount desc ) t1 ) t2 ) t3 ) t4;
-- step6. 过滤select *from ( select *, lag(acc_ratio) over() as pre_acc_ratiofrom(select *, sum(ratio) over (order by ratio desc) as acc_ratio from( select *, good_amount * 1.0 / all_amount as ratio from (select *, sum(good_amount) over () as all_amount from (select good_category, sum(amount) as good_amount from cm.tb_sale_amount group by good_category order by good_amount desc ) t1 ) t2 ) t3 ) t4) t5where pre_acc_ratio IS NULL or pre_acc_ratio < 0.90;支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!
相关文章智能推荐
1
给定用户登录表,求表中每一天的3天留存率
程技2023-08-01
2
查询连续登陆3天的用户id和登陆天数
程技2023-08-01
3
MySQL常用函数
程技2023-09-19
4
live_max_online
程技2023-08-01
5
MySQL常用30种SQL查询语句优化方法
程技2023-08-01
随机文章随机推荐
樊笼










