cs348/Project_2/answer.sql

271 lines
6.8 KiB
MySQL
Raw Normal View History

2018-10-29 15:52:17 -04:00
set serveroutput on size 32000
2018-11-05 15:36:14 -05:00
-- show errors;
2018-10-29 15:52:17 -04:00
-- Question 1: Retailer detail
create or replace procedure RetailerDetail (id IN Retailers.RetailerId%TYPE) as
2018-11-05 15:36:14 -05:00
retailer_id Retailers.RetailerId%TYPE;
retailer_name Retailers.RetailerName%TYPE;
retailer_address Retailers.Address%TYPE;
retailer_orders Orders.Count%TYPE;
retailer_bestseller_id Products.ProductId%TYPE;
retailer_bestseller Products.ProductName%TYPE;
retailer_bestseller_count Orders.Count%TYPE;
-- CURSOR retailer_cursor IS SELECT RetailerId FROM Retailers WHERE RetailerId=id;
-- retailer_entry retailer_cursor%ROWTYPE;
CURSOR order_cursor IS SELECT Count, OrderId FROM Orders WHERE RetailerId=id;
order_entry order_cursor%ROWTYPE;
2018-10-29 15:52:17 -04:00
BEGIN
2018-11-05 17:23:59 -05:00
SELECT RetailerName
INTO retailer_name
FROM Retailer
WHERE RetailerId=id;
SELECT Address
INTO retailer_address
FROM Retailer
WHERE RetailerId=id;
SELECT COUNT(OrderId)
INTO retailer_orders
FROM Orders
WHERE RetailerId=id;
SELECT ProductId
INTO retailer_bestseller_id
FROM (
SELECT ProductId, RANK()
OVER (ORDER BY Total DESC) AS Score
FROM (
SELECT ProductId, SUM(Count) AS Total
FROM Orders O
WHERE RetailerId=id
GROUP BY ProductId
)
)
WHERE Score = 1
GROUP BY ProductId;
SELECT ProductName
INTO retailer_bestseller
FROM Products
WHERE ProductId=retailer_bestseller_id;
SELECT SUM(Count)
INTO retailer_bestseller_count
FROM Orders
WHERE ProductId=retailer_bestseller_id AND RetailerId=id;
2018-11-05 15:36:14 -05:00
dbms_output.put_line('Retailer Name: ' || retailer_name);
dbms_output.put_line('Retailer Address: ' || retailer_address);
dbms_output.put_line('Retailer Total Orders: ' || retailer_orders);
2018-11-05 17:23:59 -05:00
dbms_output.put_line('Most Popular Product: ' || retailer_bestseller);
dbms_output.put_line('Total Sold: ' || retailer_bestseller_count);
2018-10-29 15:52:17 -04:00
END RetailerDetail;
/
2018-11-05 17:23:59 -05:00
-- show error procedure RetailerDetail;
2018-11-05 15:36:14 -05:00
2018-10-29 15:52:17 -04:00
-- make test cases yourselves
BEGIN
2018-11-05 17:23:59 -05:00
RetailerDetail(1); -- Inferno, 115
RetailerDetail(2); -- Macbook, 12
RetailerDetail(3); -- Nike sports jacket, 3
RetailerDetail(4); -- OnePlus 5t, 1
RetailerDetail(5); -- Pixel Book, 1
2018-10-29 15:52:17 -04:00
end;
/
-- Queation 2: Monthly delay report
create or replace procedure MonthlyDelayReport as
2018-11-05 17:23:59 -05:00
order_date VARCHAR(32);
order_count Orders.Count%TYPE;
order_year VARCHAR(32);
order_month VARCHAR(32);
retailer_name Retailers.RetailerName%TYPE;
CURSOR c_month IS
SELECT Year, Month, Total
FROM (
SELECT Year, Month, COUNT(OrderId) AS Total
FROM (
-- EXTRACT
SELECT EXTRACT(YEAR FROM OrderDate) AS Year, EXTRACT(MONTH FROM OrderDate) AS Month, OrderId, Status
FROM Orders
)
WHERE Status = 'DELAYED'
GROUP BY Year, Month
ORDER BY Year ASC, Month ASC
)
WHERE Total > 0;
CURSOR c_retailer IS
SELECT DISTINCT R.RetailerName
INTO retailer_name
FROM (
SELECT RetailerId
FROM Orders
WHERE EXTRACT(YEAR FROM OrderDate) = order_year AND EXTRACT(MONTH FROM OrderDate) = order_month AND Status = 'DELAYED'
) K, Retailers R
WHERE R.RetailerId = K.RetailerId
ORDER BY R.RetailerName ASC;
2018-10-29 15:52:17 -04:00
BEGIN
2018-11-05 17:23:59 -05:00
OPEN c_month;
LOOP
FETCH c_month INTO order_year, order_month, order_count;
EXIT WHEN c_month%NOTFOUND;
2018-11-05 17:28:41 -05:00
dbms_output.put_line('Delayed orders in ' || order_year || '-' || order_month || ': ' || order_count);
2018-11-05 17:23:59 -05:00
2018-11-05 17:28:41 -05:00
dbms_output.put_line('Retailers with delayed orders:');
2018-11-05 17:23:59 -05:00
OPEN c_retailer;
2018-11-05 15:36:14 -05:00
2018-11-05 17:23:59 -05:00
LOOP
FETCH c_retailer INTO retailer_name;
EXIT WHEN c_retailer%NOTFOUND;
dbms_output.put_line('- ' || retailer_name);
END LOOP;
CLOSE c_retailer;
END LOOP;
CLOSE c_month;
2018-10-29 15:52:17 -04:00
END MonthlyDelayReport;
/
2018-11-05 17:23:59 -05:00
2018-11-05 23:41:48 -05:00
-- show error procedure MonthlyDelayReport;
2018-11-05 17:23:59 -05:00
2018-10-29 15:52:17 -04:00
BEGIN
MonthlyDelayReport;
End;
/
-- Question 3: Find the product with least profit in each category
2018-11-05 17:28:41 -05:00
create or replace procedure LeastProfitProduct as
2018-11-05 22:22:11 -05:00
product_category Products.Category%TYPE;
product_name Products.ProductName%TYPE;
product_profit Products.ExfactoryPrice%TYPE;
CURSOR c_profit IS
SELECT ProductName, AverageProfit
FROM (
SELECT P.ProductName, AverageProfit, RANK()
OVER (ORDER BY AverageProfit ASC) AS Score
FROM (
SELECT ProductId, AVG(ProfitMargin) AS AverageProfit
FROM (
SELECT O.OrderId, O.ProductId, O.UnitPrice - P.ExFactoryPrice AS ProfitMargin
FROM Orders O, Products P
WHERE O.ProductId = P.ProductId AND P.Category = product_category
)
GROUP BY ProductId
) K, Products P
WHERE K.ProductId = P.ProductId
ORDER BY AverageProfit DESC, P.ProductName ASC
)
WHERE Score = 1;
CURSOR c_category IS
SELECT DISTINCT Category
FROM Products;
2018-11-05 17:28:41 -05:00
BEGIN
2018-11-05 22:22:11 -05:00
OPEN c_category;
LOOP
FETCH c_category INTO product_category;
EXIT WHEN c_category%NOTFOUND;
dbms_output.put_line('Least Profit in ' || product_category);
OPEN c_profit;
LOOP
FETCH c_profit INTO product_name, product_profit;
EXIT WHEN c_profit%NOTFOUND;
dbms_output.put_line('- ' || product_name || ': ' || product_profit);
END LOOP;
CLOSE c_profit;
END LOOP;
2018-11-05 15:36:14 -05:00
2018-11-05 22:22:11 -05:00
CLOSE c_category;
2018-11-05 17:28:41 -05:00
END LeastProfitProduct;
/
2018-10-29 15:52:17 -04:00
2018-11-05 17:28:41 -05:00
BEGIN
LeastProfitProduct;
END;
/
2018-10-29 15:52:17 -04:00
-- Queation 4: New table for retailer product category distribution
2018-11-05 22:22:11 -05:00
create table RetailerCatergoryTable(RetailerId integer, Electronic integer, Apparel integer, Books integer, primary key(RetailerId));
create or replace procedure RetailerProductCatergory as
2018-11-05 23:41:48 -05:00
product_category Products.Category%TYPE;
retailer_id Retailers.RetailerId%TYPE;
total_electronic integer;
total_apparel integer;
total_books integer;
CURSOR c_retailer IS
SELECT RetailerId
FROM Retailers;
CURSOR c_category IS
SELECT COUNT(OrderId) AS Total
FROM Orders O, Products P
WHERE O.ProductId = P.ProductId AND P.Category = product_category AND O.RetailerId = retailer_id;
2018-11-05 22:22:11 -05:00
BEGIN
2018-11-05 23:41:48 -05:00
OPEN c_retailer;
LOOP
FETCH c_retailer INTO retailer_id;
EXIT WHEN c_retailer%NOTFOUND;
product_category := 'electronics';
SELECT COUNT(OrderId)
INTO total_electronic
FROM Orders O, Products P
WHERE O.ProductId = P.ProductId AND P.Category = product_category AND O.RetailerId = retailer_id;
product_category := 'apparel';
SELECT COUNT(OrderId)
INTO total_apparel
FROM Orders O, Products P
WHERE O.ProductId = P.ProductId AND P.Category = product_category AND O.RetailerId = retailer_id;
product_category := 'books';
SELECT COUNT(OrderId)
INTO total_books
FROM Orders O, Products P
WHERE O.ProductId = P.ProductId AND P.Category = product_category AND O.RetailerId = retailer_id;
INSERT INTO RetailerCatergoryTable (RetailerId, Electronic, Apparel, Books) VALUES (retailer_id, total_electronic, total_apparel, total_books);
END LOOP;
2018-11-05 15:36:14 -05:00
2018-11-05 23:41:48 -05:00
CLOSE c_retailer;
2018-11-05 22:22:11 -05:00
END RetailerProductCatergory;
/
2018-10-29 15:52:17 -04:00
2018-11-05 23:41:48 -05:00
show error procedure RetailerProductCatergory;
2018-11-05 22:22:11 -05:00
BEGIN
RetailerProductCatergory;
END;
/
select * from RetailerCatergoryTable;
2018-10-29 15:52:17 -04:00
2018-11-05 22:22:11 -05:00
drop table RetailerCatergoryTable;
2018-10-29 15:52:17 -04:00
-- Question 5: Exception Handle
2018-11-05 23:41:48 -05:00
create or replace procedure CustomerProductInfo(cid IN Customers.CustomerId%TYPE, pid IN Products.ProductId%TYPE) as
2018-10-29 15:52:17 -04:00
2018-11-05 23:41:48 -05:00
BEGIN
2018-11-05 15:36:14 -05:00
2018-11-05 23:41:48 -05:00
EXCEPTION
2018-11-05 15:36:14 -05:00
2018-11-05 23:41:48 -05:00
END CustomerProductInfo;
/
2018-10-29 15:52:17 -04:00
2018-11-05 23:41:48 -05:00
BEGIN
CustomerProductInfo(1,1);
END;
/
2018-10-29 15:52:17 -04:00
2018-11-05 23:41:48 -05:00
BEGIN
CustomerProductInfo(-1,1);
END;
/