-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall_solutions.sql
More file actions
281 lines (260 loc) · 9.29 KB
/
all_solutions.sql
File metadata and controls
281 lines (260 loc) · 9.29 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/************************************************************************************************
REQUEST 1: Provide the list of markets in which customer "Atliq Exclusive" operates its
business in the APAC region.
************************************************************************************************/
SELECT DISTINCT market
FROM dim_customer
WHERE
customer = "Atliq Exclusive"
AND region = "APAC";
/************************************************************************************************
REQUEST 2: What is the percentage of unique product increase in 2021 vs. 2020?
************************************************************************************************/
WITH product_counts AS (
SELECT
-- Count distinct products for the year 2020
COUNT(DISTINCT CASE WHEN fiscal_year = 2020 THEN product_code END) AS unique_products_2020,
-- Count distinct products for the year 2021
COUNT(DISTINCT CASE WHEN fiscal_year = 2021 THEN product_code END) AS unique_products_2021
FROM
fact_sales_monthly
)
SELECT
unique_products_2020,
unique_products_2021,
-- Calculate the percentage change
ROUND((unique_products_2021 - unique_products_2020) * 100.0 / unique_products_2020, 2) AS percentage_chg
FROM
product_counts;
/************************************************************************************************
REQUEST 3: Provide a report with all the unique product counts for each segment and
sort them in descending order of product counts.
************************************************************************************************/
SELECT
segment,
COUNT(DISTINCT product_code) AS product_count
FROM
dim_product
GROUP BY
segment
ORDER BY
product_count DESC;
/************************************************************************************************
REQUEST 4: Which segment had the most increase in unique products in 2021 vs 2020?
************************************************************************************************/
WITH product_counts_by_segment AS (
SELECT
p.segment,
-- Count distinct products for 2020 in each segment
COUNT(DISTINCT CASE WHEN s.fiscal_year = 2020 THEN s.product_code END) AS product_count_2020,
-- Count distinct products for 2021 in each segment
COUNT(DISTINCT CASE WHEN s.fiscal_year = 2021 THEN s.product_code END) AS product_count_2021
FROM
fact_sales_monthly s
JOIN
dim_product p ON s.product_code = p.product_code
GROUP BY
p.segment
)
SELECT
segment,
product_count_2020,
product_count_2021,
-- Calculate the difference
(product_count_2021 - product_count_2020) AS difference
FROM
product_counts_by_segment
ORDER BY
difference DESC;
/************************************************************************************************
REQUEST 5: Get the products that have the highest and lowest manufacturing costs.
************************************************************************************************/
WITH ranked_costs AS (
SELECT
p.product_code,
p.product,
m.manufacturing_cost,
-- Rank products by cost from lowest to highest
RANK() OVER (ORDER BY m.manufacturing_cost ASC) as rank_asc,
-- Rank products by cost from highest to lowest
RANK() OVER (ORDER BY m.manufacturing_cost DESC) as rank_desc
FROM
fact_manufacturing_cost m
JOIN
dim_product p ON m.product_code = p.product_code
)
SELECT
product_code,
product,
manufacturing_cost
FROM
ranked_costs
WHERE
-- Select the #1 ranked in both categories
rank_asc = 1 OR rank_desc = 1;
/************************************************************************************************
REQUEST 6: Generate a report which contains the top 5 customers who received an
average high pre_invoice_discount_pct for the fiscal year 2021 and in the
Indian market.
************************************************************************************************/
WITH CustomerDiscounts AS (
-- First, calculate the average discount for each customer in the specified market and year
SELECT
c.customer_code,
c.customer,
AVG(pid.pre_invoice_discount_pct) AS average_discount_percentage
FROM
fact_pre_invoice_deductions pid
JOIN
dim_customer c ON pid.customer_code = c.customer_code
WHERE
pid.fiscal_year = 2021
AND c.market = 'India'
GROUP BY
c.customer_code,
c.customer
),
RankedCustomers AS (
-- Next, rank the customers based on their average discount
SELECT
customer_code,
customer,
average_discount_percentage,
DENSE_RANK() OVER (ORDER BY average_discount_percentage DESC) as discount_rank
FROM
CustomerDiscounts
)
-- Finally, select the top 5 ranked customers
SELECT
customer_code,
customer,
ROUND(average_discount_percentage, 4) AS average_discount_percentage
FROM
RankedCustomers
WHERE
discount_rank <= 5;
/************************************************************************************************
REQUEST 7: Get the complete report of the Gross sales amount for the customer "Atliq
Exclusive" for each month.
************************************************************************************************/
WITH MonthlySales AS (
-- First, join the tables to get the gross price for each sale and filter for the specific customer
SELECT
s.date,
(s.sold_quantity * g.gross_price) AS gross_sales_amount
FROM
fact_sales_monthly s
JOIN
dim_customer c ON s.customer_code = c.customer_code
JOIN
fact_gross_price g ON s.product_code = g.product_code
AND s.fiscal_year = g.fiscal_year
WHERE
c.customer = 'Atliq Exclusive'
)
-- Now, aggregate the results by month and year
SELECT
DATE_FORMAT(date, '%M') AS Month, -- Format date to get the full month name
YEAR(date) AS Year,
ROUND(SUM(gross_sales_amount), 2) AS `Gross sales Amount`
FROM
MonthlySales
GROUP BY
Year, Month, MONTH(date) -- Group by month number as well for correct sorting
ORDER BY
Year, MONTH(date); -- Order by month number to ensure chronological order
/************************************************************************************************
REQUEST 8: In which quarter of 2020, got the maximum total_sold_quantity?
************************************************************************************************/
SELECT
-- Format the quarter number as 'Q1', 'Q2', etc. for better readability
CONCAT('Q', QUARTER(date)) AS Quarter,
SUM(sold_quantity) AS total_sold_quantity
FROM
fact_sales_monthly
WHERE
fiscal_year = 2020
GROUP BY
Quarter
ORDER BY
total_sold_quantity DESC;
/************************************************************************************************
REQUEST 9: Which channel helped to bring more gross sales in the fiscal year 2021
and the percentage of contribution?
************************************************************************************************/
WITH ChannelGrossSales AS (
-- First, calculate the gross sales for each channel in the fiscal year 2021
SELECT
c.channel,
SUM(s.sold_quantity * g.gross_price) AS total_gross_sales
FROM
fact_sales_monthly s
JOIN
dim_customer c ON s.customer_code = c.customer_code
JOIN
fact_gross_price g ON s.product_code = g.product_code
AND s.fiscal_year = g.fiscal_year
WHERE
s.fiscal_year = 2021
GROUP BY
c.channel
)
-- Now, calculate the contribution percentage for each channel
SELECT
channel,
-- Format gross sales in millions with rounding
ROUND(total_gross_sales / 1000000, 2) AS gross_sales_mln,
-- Calculate percentage contribution using a window function to get the grand total
ROUND(
total_gross_sales * 100.0 / SUM(total_gross_sales) OVER (),
2
) AS percentage
FROM
ChannelGrossSales
ORDER BY
percentage DESC;
/************************************************************************************************
REQUEST 10: Get the Top 3 products in each division that have a high
total_sold_quantity in the fiscal_year 2021.
************************************************************************************************/
WITH ProductSales AS (
-- First, calculate the total sold quantity for each product in the fiscal year 2021
SELECT
p.division,
p.product_code,
p.product,
SUM(s.sold_quantity) AS total_sold_quantity
FROM
fact_sales_monthly s
JOIN
dim_product p ON s.product_code = p.product_code
WHERE
s.fiscal_year = 2021
GROUP BY
p.division,
p.product_code,
p.product
),
RankedProducts AS (
-- Next, rank the products within each division based on their total sold quantity
SELECT
division,
product_code,
product,
total_sold_quantity,
DENSE_RANK() OVER (PARTITION BY division ORDER BY total_sold_quantity DESC) as rank_order
FROM
ProductSales
)
-- Finally, select the top 3 ranked products from each division
SELECT
division,
product_code,
product,
total_sold_quantity,
rank_order
FROM
RankedProducts
WHERE
rank_order <= 3;
```