-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6_top_5_customer_discounts.sql
More file actions
36 lines (36 loc) · 1016 Bytes
/
6_top_5_customer_discounts.sql
File metadata and controls
36 lines (36 loc) · 1016 Bytes
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
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;