-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathben_Lab | SQL Iterations.sql
More file actions
113 lines (83 loc) · 3.17 KB
/
ben_Lab | SQL Iterations.sql
File metadata and controls
113 lines (83 loc) · 3.17 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
USE Sakila;
/*Write a query to find what is the total business done by each store.*/
SELECT s.store_id, SUM(pa.amount) AS total_business
FROM store s
LEFT JOIN inventory i ON s.store_id = i.store_id
LEFT JOIN rental r ON i.inventory_id = r.inventory_id
LEFT JOIN payment pa ON r.rental_id = pa.rental_id
GROUP BY s.store_id;
/* Convert the previous query into a stored procedure.*/
DELIMITER //
CREATE PROCEDURE Business_Store()
BEGIN
SELECT s.store_id, SUM(pa.amount) AS total_business
FROM store s
LEFT JOIN inventory i ON s.store_id = i.store_id
LEFT JOIN rental r ON i.inventory_id = r.inventory_id
LEFT JOIN payment pa ON r.rental_id = pa.rental_id
GROUP BY s.store_id;
END //
DELIMITER ;
CALL Business_Store();
/*Convert the previous query into a stored procedure that takes the
input for store_id and displays the total sales for that store*/
DELIMITER //
CREATE PROCEDURE Business_Store(IN input_store_id INT)
BEGIN
SELECT s.store_id, SUM(pa.amount) AS total_business
FROM store s
LEFT JOIN inventory i ON s.store_id = i.store_id
LEFT JOIN rental r ON i.inventory_id = r.inventory_id
LEFT JOIN payment pa ON r.rental_id = pa.rental_id
WHERE s.store_id = input_store_id
GROUP BY s.store_id;
END //
DELIMITER ;
CALL Business_Store(1);
/*Update the previous query. Declare a variable total_sales_value of float type,
that will store the returned result (of the total sales amount for the store).
Call the stored procedure and print the results.*/
DELIMITER //
CREATE PROCEDURE Business_Store(IN input_store_id INT)
BEGIN
DECLARE total_sales_value FLOAT;
SELECT SUM(pa.amount) INTO total_sales_value
FROM store s
LEFT JOIN inventory i ON s.store_id = i.store_id
LEFT JOIN rental r ON i.inventory_id = r.inventory_id
LEFT JOIN payment pa ON r.rental_id = pa.rental_id
WHERE s.store_id = input_store_id;
SELECT total_sales_value AS 'Total Business';
END //
DELIMITER ;
-- Call the stored procedure and print the results
CALL Business_Store(1);
CALL Business_Store(2);
/*In the previous query, add another variable flag. If the total sales value for the store is over 30.000,
then label it as green_flag, otherwise label is as red_flag. Update the stored procedure that takes an
input as the store_id and returns total sales value for that store and flag value.*/
DELIMITER //
CREATE PROCEDURE Business_Store(IN input_store_id INT)
BEGIN
DECLARE total_sales_value FLOAT;
DECLARE flag VARCHAR(20);
-- Calculate total sales for the given store_id
SELECT SUM(pa.amount) INTO total_sales_value
FROM store s
LEFT JOIN inventory i ON s.store_id = i.store_id
LEFT JOIN rental r ON i.inventory_id = r.inventory_id
LEFT JOIN payment pa ON r.rental_id = pa.rental_id
WHERE s.store_id = input_store_id;
-- Determine the flag based on the total sales value
IF total_sales_value > 30000 THEN
SET flag = 'green_flag';
ELSE
SET flag = 'red_flag';
END IF;
-- Print or do something with the total_sales_value and flag
SELECT total_sales_value AS 'Total Business', flag AS 'Flag Status';
END //
DELIMITER ;
-- Call the stored procedure and print the results
CALL Business_Store(1);
CALL Business_Store(2);