-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL_Queries.sql
More file actions
217 lines (199 loc) · 5.55 KB
/
SQL_Queries.sql
File metadata and controls
217 lines (199 loc) · 5.55 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
/* These are the queries used according to the demand of the flower shop */
USE mm_cpsc5910team03;
/* Find the customer buying the most expensive combo in a single time. */
SELECT
c.cust_id AS 'CustomerID',
CONCAT(c.f_name, ' ', c.l_name) AS 'Customer Name',
o.total_price AS 'Product of Highest Price'
FROM
cust_info c,
order_info o
WHERE c.cust_id = o.cust_id
AND o.total_price = (
SELECT
MAX(o.total_price)
FROM
order_info o);
/* Show all people who received the flowers instead of the customers. */
SELECT
CONCAT(c.f_name, ' ', c.l_name) AS 'Customer Name',
CONCAT(s.recipient_f_name, ' ', s.recipient_l_name) AS 'Recipient Name',
s.est_arrive_time AS 'Estimated Arrival Time'
FROM
cust_info c,
order_info o,
shipping s
WHERE s.ship_id = o.ship_id
AND o.cust_id = c.cust_id
AND c.f_name != s. recipient_f_name
AND c.l_name != s.recipient_l_name
AND s.est_arrive_time > 0;
/* Show all next-day delivery orders. */
SELECT
CONCAT(c.f_name, ' ', c.l_name) AS 'Customer Name',
o.order_id AS 'Order ID',
c.last_order_time AS 'Last order time',
s.est_arrive_time AS 'Estimated Arrival Time'
FROM
cust_info c,
order_info o,
shipping s
WHERE c.cust_id = o.cust_id
And o.ship_id = s.ship_id
And YEAR(c.last_order_time) = YEAR(s.est_arrive_time)
AND MONTH(c.last_order_time) = MONTH(s.est_arrive_time)
AND DAY(c.last_order_time) +1 = DAY(s.est_arrive_time);
/* Overview of total sales amount for each year */
CREATE VIEW SalesYearComparison AS
SELECT
YEAR(o.order_date) AS 'Sales Year',
SUM(d.price_after_disc) AS 'Current Sales Amount',
SUM(d.sale_price) AS 'Sales Amount Before Discount',
LEAD(sum(d.price_after_disc), 1, NULL)
OVER (order by o.order_date) AS 'Next Year Sales Amount',
LEAD(sum(d.sale_price), 1, NULL)
OVER (order by o.order_date) AS 'Next Year Sales Amount Before Discount'
FROM
order_info o,
order_detail d
WHERE o.order_id = d.order_id
GROUP BY YEAR(o.order_date);
SELECT * FROM SalesYearComparison;
/* Show the age group with the most popular product. */
SELECT
p.product_name AS 'Product Name',
COUNT(d.product_id) AS 'Product Quantity',
CONCAT(TRUNCATE(YEAR(CURDATE()) - YEAR(c.dob), -1),
'-',
TRUNCATE(YEAR(CURDATE()) - YEAR(c.dob),-1)+9) AS 'Age Group',
CASE
WHEN p.category = 'Event' THEN 'Event Service'
ELSE 'In-Home Delivery'
END AS 'Product Category'
FROM
cust_info c,
order_detail d,
product p
WHERE c.cust_id = d.cust_id
AND d.product_id = p.product_id
GROUP BY d.product_id, 'Age Group'
ORDER BY 'Product Quantity';
/* Show the manager who has the highest review score. */
SELECT e.manager_id AS 'Manager ID',
CONCAT(e.f_name, ' ', e.l_name) AS 'Manager Name',
e.specialty AS 'Specialty',
ROUND(AVG(r.score),1) AS 'Average Review Score',
COUNT(r.score) AS 'Number of Reviews'
FROM
event_manager e,
cust_review r,
event_service s,
order_info o,
cust_info c
WHERE e.manager_id = s.manager_id
AND s.service_id = o.service_id
AND o.cust_id = c.cust_id
AND c.cust_id = r.cust_id
AND o.order_type = 'event service'
AND o.order_status = 'Completed'
GROUP BY e.manager_id
ORDER BY AVG(r.score) DESC LIMIT 1;
/* Find the lowest price of each product and the corresponding supplier? */
SELECT DISTINCT
w.product_id,
(SELECT
product_name
FROM
product p
WHERE p.product_id = w.product_id ) AS 'Product Name',
MIN(w.warehousing_price) AS 'Min Price Of Product',
s.supplier_name AS 'Supplier Name'
FROM
warehousing AS w
JOIN supplier AS s ON w.supplier_id = s.supplier_id
GROUP BY product_id;
/* How much of each product do we have in our warehouse?*/
SELECT DISTINCT
product_id AS 'Product ID',
(SELECT
product_name
FROM
product p
WHERE p.product_id = w.product_id ) AS 'Product Name',
SUM(warehousing_amt) AS Amount
FROM
warehousing as w
GROUP BY product_id
ORDER BY SUM(warehousing_amt) DESC;
/* How many suppliers do we have per product? */
SELECT
product_id AS 'Product ID',
(SELECT
product_name
FROM
product p
WHERE p.product_id = w.product_id ) AS 'Product Name',
COUNT(*) AS 'Number of Suppliers per product'
FROM
warehousing as w
GROUP BY product_id;
/* How many members joined our website per year? */
SELECT
YEAR(mem_since) AS YEAR,
COUNT(*) AS 'Num of Member Joined Per Year'
FROM
cust_info
GROUP BY YEAR(mem_since)
ORDER BY COUNT(*) DESC;
/* Stroed Procedure 1*/
DELIMITER $$
CREATE PROCEDURE ProductSalesByYearMonth
(IN ProductName Varchar(60),
IN SalesYear INT,
IN SalesMonth INT)
BEGIN
SELECT
p.product_id AS 'Product ID',
p.product_name AS 'Product Name',
p.category AS 'Category',
YEAR(o.order_date) AS 'Sales Year',
MONTH(o.order_date) AS 'Sales Month',
o.order_type AS 'Order Type',
sum((o.total_price + s.shippment_fee)) AS 'Total Sales Amount'
FROM
product p,
order_info o,
shipment_fee s,
order_detail d
WHERE
p.product_id = d.product_id
AND o.order_id = d.order_id
AND o.shipment_fee_id = s.shipment_fee_id
AND p.product_name = ProductName
AND YEAR(o.order_date) = SalesYear
AND MONTH(o.order_date) = SalesMonth
GROUP BY d.product_id;
END$$
DELIMITER ;
/* Test trial for getting sales amount for Rose in Feb 2020*/
CALL ProductSalesByYearMonth('Rose', 2020, 02);
/*Stored Procedure 2*/
DELIMITER $$
CREATE PROCEDURE FindCustomerInfo
(IN ID int)
BEGIN
SELECT
c.cust_id AS 'Customer ID',
CONCAT(c.f_name, ' ', c.l_name) AS 'Customer Name',
COUNT(*) AS 'Total Order Number'
FROM
cust_info c,
order_info o
WHERE
c.cust_id = o.cust_id
And o.cust_id = ID
GROUP BY c.cust_id;
END$$
DELIMITER ;
/*Test trial for getting total shipping fee for cust_id = 1*/
CALL FindCustomerInfo(1);