Files
sql_advanced/lab2-9/task2.sql
T
Vitalii Litvinchuk 6deed0469a first commit
2026-05-04 23:15:09 +03:00

42 lines
1.8 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#####################################################
###################### Task 2: ##################
1. Створіть збережену процедуру, яка довзволить вивести вартість замовлення із врахуванням знижки.
Як вхідний параметр процедура повинна використовувати значення ідентифікатора замовлення (OrderID).
Умови знижки:
якщо кількість товару у замовлення >= 20, тоді знижка становить 20%;
якщо кількість товару у замовленні < 20 і >= 10, тоді знижка становить 10%;
в іншому випадку знижка не передбачена.
2. Викличте створену процедуру зі значення ідентифікатора замовлення OrderID = 5.
#####################################################
drop procedure if exists CalculateDiscountedCost;
create procedure CalculateDiscountedCost(in order_id int)
begin
declare v_cost decimal(10,2);
declare v_discount decimal(5,2);
declare v_final_cost decimal(10,2);
declare v_quantity int;
select Cost, Quantity into v_cost, v_quantity
from Orders
where OrderID = order_id;
if v_quantity >= 20 then
set v_discount = 0.20;
elseif v_quantity >= 10 then
set v_discount = 0.10;
else
set v_discount = 0.00;
end if;
set v_final_cost = v_cost * (1 - v_discount);
select v_final_cost as FinalCost, v_cost as Cost, v_discount as Discount, v_quantity as Quantity, v_cost - v_cost * (1 - v_discount) as DiscountAmount;
end
set @OrderID = 8;
call CalculateDiscountedCost (@OrderID);