forked from ADB-course/20240819-20241125-adb-bbit2-2-classroom-semester-project-BBT3104-SemesterProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.sql
More file actions
31 lines (26 loc) · 765 Bytes
/
script.sql
File metadata and controls
31 lines (26 loc) · 765 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
-- Write your SQL code here
--1.Calculate Total Cost for an Order
DELIMITER //
CREATE FUNCTION calculate_order_total (order_id_in INT)
RETURNS DECIMAL(10, 2)
DETERMINISTIC
BEGIN
DECLARE total_cost DECIMAL(10, 2) DEFAULT 0;
SELECT SUM(m.price * oi.quantity) INTO total_cost
FROM Order_Item oi
INNER JOIN Menu m ON oi.dish_id = m.dish_id
WHERE oi.order_id = order_id_in;
RETURN total_cost;
END //
DELIMITER ;
--2.Calculate Average Menu Item Cost by Category
DELIMITER //
CREATE FUNCTION calculate_average_menu_cost (category_in VARCHAR(50))
RETURNS DECIMAL(10, 2)
DETERMINISTIC
BEGIN
DECLARE avg_cost DECIMAL(10, 2);
SELECT AVG(price) INTO avg_cost FROM Menu WHERE category = category_in;
RETURN avg_cost;
END //
DELIMITER ;