forked from thetadog/fifo_ims_database
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4_SKU_Sale_Profit_Stat.sql
171 lines (162 loc) · 6.92 KB
/
4_SKU_Sale_Profit_Stat.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
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
USE ims_SKU;
-- display weekly sale amount by item and store
# this procedure WILL NOT display store that hasn't made any sale
DROP PROCEDURE IF EXISTS get_weekly_sale_by_item;
DELIMITER //
CREATE PROCEDURE get_weekly_sale_by_item(IN input_store_id INT,
IN input_item_id INT)
BEGIN
SELECT rs.store_id,
rs.store_address,
i.item_id,
i.item_name,
YEARWEEK(sale.sale_date) AS year_week,
SUM(shs.sale_quantity) AS sale_quantity,
SUM(shs.sale_quantity * shs.unit_sale_price) AS sale_amt
FROM retail_store rs
JOIN supply_order so on rs.store_id = so.store_id
JOIN sku S on so.order_id = S.order_id
JOIN item i on S.item_id = i.item_id
JOIN sale_has_sku shs on S.sku_id = shs.sku_id
JOIN sale on shs.sale_id = sale.sale_id
WHERE 1 = 1 # a delivery_date check is unnecessary since no sale can be inserted if not delivered
AND (
CASE
WHEN input_store_id IS NOT NULL THEN rs.store_id = input_store_id
ELSE 1 = 1
END)
AND (
CASE
WHEN input_item_id IS NOT NULL THEN i.item_id = input_item_id
ELSE 1 = 1
END)
GROUP BY rs.store_id, rs.store_address, i.item_id, i.item_name, year_week
ORDER BY year_week DESC, rs.store_id, i.item_id;
END//
DELIMITER ;
CALL get_weekly_sale_by_item(null, null);
-- display weekly profits by item and store
DROP PROCEDURE IF EXISTS get_weekly_profit_by_item;
DELIMITER //
CREATE PROCEDURE get_weekly_profit_by_item(IN input_store_id INT,
IN input_item_id INT)
BEGIN
SELECT rs.store_id,
rs.store_address,
i.item_id,
i.item_name,
SUM(shs.sale_quantity * (shs.unit_sale_price - S.unit_cost)) AS profit,
YEARWEEK(sale.sale_date) AS sale_week
FROM retail_store rs
JOIN supply_order so ON rs.store_id = so.store_id
JOIN sku S on S.order_id = so.order_id
JOIN item i on S.item_id = i.item_id
JOIN sale_has_sku shs on S.sku_id = shs.sku_id
JOIN sale on shs.sale_id = sale.sale_id
WHERE 1 = 1
AND (
CASE # set condition for store_id
WHEN input_store_id IS NOT NULL THEN rs.store_id = input_store_id
ELSE 1 = 1
END)
AND (
CASE # set condition for item_id
WHEN input_item_id IS NOT NULL THEN i.item_id = input_item_id
ELSE 1 = 1
END)
GROUP BY rs.store_id, rs.store_address, i.item_id, i.item_name, sale_week
ORDER BY sale_week DESC, rs.store_id, i.item_id, profit;
END//
DELIMITER ;
CALL get_weekly_profit_by_item(null, null);
-- get the total amount of sales by item & store
# a time range filter can be applied by setting start_date and end_date
DROP PROCEDURE IF EXISTS get_total_sale_by_item;
DELIMITER //
CREATE PROCEDURE get_total_sale_by_item(IN input_store_id INT,
IN input_item_id INT,
IN input_start_date DATE,
IN input_end_date DATE)
BEGIN
SELECT rs.store_id,
rs.store_address,
i.item_id,
i.item_name,
SUM(shs.sale_quantity) AS sale_quantity,
SUM(shs.sale_quantity * shs.unit_sale_price) AS sale_amt,
IF(input_start_date <= input_end_date, YEARWEEK(input_start_date), NULL) AS FROM_WEEK,
IF(input_start_date <= input_end_date, YEARWEEK(input_end_date), NULL) AS TO_WEEK
FROM retail_store rs
JOIN supply_order so on rs.store_id = so.store_id
JOIN sku S on so.order_id = S.order_id
JOIN item i on S.item_id = i.item_id
JOIN sale_has_sku shs on S.sku_id = shs.sku_id
JOIN sale on shs.sale_id = sale.sale_id
WHERE 1 = 1 # a delivery_date check is unnecessary since no sale can be inserted if not delivered
AND (
CASE
WHEN input_store_id IS NOT NULL THEN rs.store_id = input_store_id
ELSE 1 = 1
END)
AND (
CASE
WHEN input_item_id IS NOT NULL THEN i.item_id = input_item_id
ELSE 1 = 1
END)
AND (
CASE # set condition for week range
WHEN input_start_date IS NOT NULL AND input_end_date IS NOT NULL AND
input_start_date <= input_end_date THEN
YEARWEEK(sale.sale_date) BETWEEN YEARWEEK(input_start_date) AND YEARWEEK(input_end_date)
ELSE 1 = 1
END)
GROUP BY rs.store_id, rs.store_address, i.item_id, i.item_name, FROM_WEEK, TO_WEEK
ORDER BY rs.store_id, i.item_id, sale_quantity, sale_amt;
END//
DELIMITER ;
call get_total_sale_by_item(null, null, null, null);
-- get the total profit by item & store
# a time range filter can be applied by setting start_date and end_date
DROP PROCEDURE IF EXISTS get_total_profit_by_item;
DELIMITER //
CREATE PROCEDURE get_total_profit_by_item(IN input_store_id INT,
IN input_item_id INT,
IN input_start_date DATE,
IN input_end_date DATE)
BEGIN
SELECT rs.store_id,
rs.store_address,
i.item_id,
i.item_name,
SUM(shs.sale_quantity * (shs.unit_sale_price - S.unit_cost)) AS profit,
IF(input_start_date <= input_end_date, YEARWEEK(input_start_date), NULL) AS FROM_WEEK,
IF(input_start_date <= input_end_date, YEARWEEK(input_end_date), NULL) AS TO_WEEK
FROM retail_store rs
JOIN supply_order so ON rs.store_id = so.store_id
JOIN sku S on S.order_id = so.order_id
JOIN item i on S.item_id = i.item_id
JOIN sale_has_sku shs on S.sku_id = shs.sku_id
JOIN sale on shs.sale_id = sale.sale_id
WHERE 1 = 1
AND (
CASE # set condition for store_id
WHEN input_store_id IS NOT NULL THEN rs.store_id = input_store_id
ELSE 1 = 1
END)
AND (
CASE # set condition for item_id
WHEN input_item_id IS NOT NULL THEN i.item_id = input_item_id
ELSE 1 = 1
END)
AND (
CASE # set condition for week range
WHEN input_start_date IS NOT NULL AND input_end_date IS NOT NULL AND
input_start_date <= input_end_date THEN
YEARWEEK(sale.sale_date) BETWEEN YEARWEEK(input_start_date) AND YEARWEEK(input_end_date)
ELSE 1 = 1
END)
GROUP BY rs.store_id, rs.store_address, i.item_id, i.item_name
ORDER BY rs.store_id, i.item_id, profit;
END//
DELIMITER ;
CALL get_total_profit_by_item(null, null, null, null);