-
Notifications
You must be signed in to change notification settings - Fork 0
/
01 Finance Analytics.sql
142 lines (113 loc) · 4.12 KB
/
01 Finance Analytics.sql
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
# Finance Analytics
-- a. first grab customer codes for Croma india
SELECT * FROM dim_customer WHERE customer like "%croma%" AND market="india";
-- b. Get all the sales transaction data from fact_sales_monthly table for that customer(croma: 90002002) in the fiscal_year 2021
SELECT * FROM fact_sales_monthly
WHERE
customer_code=90002002 AND
YEAR(DATE_ADD(date, INTERVAL 4 MONTH))=2021
ORDER BY date asc
LIMIT 100000;
-- c. create a function 'get_fiscal_year' to get fiscal year by passing the date
CREATE FUNCTION `get_fiscal_year`(calendar_date DATE)
RETURNS int
DETERMINISTIC
BEGIN
DECLARE fiscal_year INT;
SET fiscal_year = YEAR(DATE_ADD(calendar_date, INTERVAL 4 MONTH));
RETURN fiscal_year;
END
-- d. Replacing the function created in the step:b
SELECT * FROM fact_sales_monthly
WHERE
customer_code=90002002 AND
get_fiscal_year(date)=2021
ORDER BY date asc
LIMIT 100000;
Gross Sales Report: Monthly Product Transactions
-- a. Perform joins to pull product information
SELECT s.date, s.product_code, p.product, p.variant, s.sold_quantity
FROM fact_sales_monthly s
JOIN dim_product p
ON s.product_code=p.product_code
WHERE
customer_code=90002002 AND
get_fiscal_year(date)=2021
LIMIT 1000000;
-- b. Performing join with 'fact_gross_price' table with the above query and generating required fields
SELECT
s.date,
s.product_code,
p.product,
p.variant,
s.sold_quantity,
g.gross_price,
ROUND(s.sold_quantity*g.gross_price,2) as gross_price_total
FROM fact_sales_monthly s
JOIN dim_product p
ON s.product_code=p.product_code
JOIN fact_gross_price g
ON g.fiscal_year=get_fiscal_year(s.date)
AND g.product_code=s.product_code
WHERE
customer_code=90002002 AND
get_fiscal_year(s.date)=2021
LIMIT 1000000;
Gross Sales Report: Total Sales Amount
-- Generate monthly gross sales report for Croma India for all the years
SELECT
s.date,
SUM(ROUND(s.sold_quantity*g.gross_price,2)) as monthly_sales
FROM fact_sales_monthly s
JOIN fact_gross_price g
ON g.fiscal_year=get_fiscal_year(s.date) AND g.product_code=s.product_code
WHERE
customer_code=90002002
GROUP BY date;
Stored Procedures: Monthly Gross Sales Report
-- Generate monthly gross sales report for any customer using stored procedure
CREATE PROCEDURE `get_monthly_gross_sales_for_customer`(
in_customer_codes TEXT
)
BEGIN
SELECT
s.date,
SUM(ROUND(s.sold_quantity*g.gross_price,2)) as monthly_sales
FROM fact_sales_monthly s
JOIN fact_gross_price g
ON g.fiscal_year=get_fiscal_year(s.date)
AND g.product_code=s.product_code
WHERE
FIND_IN_SET(s.customer_code, in_customer_codes) > 0
GROUP BY s.date
ORDER BY s.date DESC;
END
Stored Procedure: Market Badge
-- Write a stored proc that can retrieve market badge. i.e. if total sold quantity > 5 million that market is considered "Gold" else "Silver"
CREATE PROCEDURE `get_market_badge`(
IN in_market VARCHAR(45),
IN in_fiscal_year YEAR,
OUT out_level VARCHAR(45)
)
BEGIN
DECLARE qty INT DEFAULT 0;
# Default market is India
IF in_market = "" THEN
SET in_market="India";
END IF;
# Retrieve total sold quantity for a given market in a given year
SELECT
SUM(s.sold_quantity) INTO qty
FROM fact_sales_monthly s
JOIN dim_customer c
ON s.customer_code=c.customer_code
WHERE
get_fiscal_year(s.date)=in_fiscal_year AND
c.market=in_market;
# Determine Gold vs Silver status
IF qty > 5000000 THEN
SET out_level = 'Gold';
ELSE
SET out_level = 'Silver';
END IF;
END