求占据前90%销售额的商品类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
-- 求占据前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_amount
from cm.tb_sale_amount
group by good_category
order by good_amount desc;

-- step2. 求全部商品的总销售额,为了step3求各种商品的占比,需要先求和。注意:求总和时,窗口值既不排序也不进行分组
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;

-- step3. 求占比
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;

-- step4. 求累计占比,注意:求累计值时,一定要进行排序
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
) as t1
) t2
) t3;

-- step5. 求前一行的累计占比
select
*,
lag(acc_ratio) over() as pre_acc_ratio
from(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_ratio
from(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
) t5
where pre_acc_ratio IS NULL or pre_acc_ratio < 0.90;

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 richffan@outlook.com

文章标题:求占据前90%销售额的商品类型

字数:520

本文作者:Rich Fan

发布时间:2023-08-01, 00:00:00

最后更新:2024-02-27, 08:17:39

原始链接:http://fanrich.github.io/2023/07/31/SQL/90_percent_contribution_goods/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。