0% found this document useful (0 votes)
75 views

Oracle PL SQL - Table Funtions - Lab Programs

This document defines a PL/SQL function called get_total_sales that accepts a year as a parameter and returns the total sales amount for that year. It does this by querying the order_items and orders tables to sum the unit prices multiplied by quantities for orders in the given year that have a status of "Shipped". The function is then called in three places: to assign the 2017 sales to a variable and display it, to check if 2017 sales exceed $10M and display a message, and to select the 2017 sales amount from the dual table.

Uploaded by

raaman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Oracle PL SQL - Table Funtions - Lab Programs

This document defines a PL/SQL function called get_total_sales that accepts a year as a parameter and returns the total sales amount for that year. It does this by querying the order_items and orders tables to sum the unit prices multiplied by quantities for orders in the given year that have a status of "Shipped". The function is then called in three places: to assign the 2017 sales to a variable and display it, to check if 2017 sales exceed $10M and display a message, and to select the 2017 sales amount from the dual table.

Uploaded by

raaman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

CREATE OR REPLACE FUNCTION get_total_sales(

in_year PLS_INTEGER
)
RETURN NUMBER
IS
l_total_sales NUMBER := 0;
BEGIN
-- get total sales
SELECT SUM(unit_price * quantity)
INTO l_total_sales
FROM order_items
INNER JOIN orders USING (order_id)
WHERE status = 'Shipped'
GROUP BY EXTRACT(YEAR FROM order_date)
HAVING EXTRACT(YEAR FROM order_date) = in_year;

-- return the total sales


RETURN l_total_sales;
END;

DECLARE
l_sales_2017 NUMBER := 0;
BEGIN
l_sales_2017 := get_total_sales (2017);
DBMS_OUTPUT.PUT_LINE('Sales 2017: ' || l_sales_2017);
END;

BEGIN
IF get_total_sales (2017) > 10000000 THEN
DBMS_OUTPUT.PUT_LINE('Sales 2017 is above target');
END IF;
END;

SELECT
get_total_sales(2017)
FROM
dual;

You might also like