CREATE TABLE corder ( "Invoice Number " Varchar2(4), "Invoice Date " Date, "Customer Code " Number(1), "Product Code " Number(1), "Quantity Sold " Number(3) )
CREATE OR REPLACE PROCEDURE find_sale_purchase AS
CURSOR c1 IS
SELECT distinct("Customer Code ")
FROM corder;
CURSOR c2(v_ccode number) IS
SELECT *
FROM corder
WHERE "Customer Code "=v_ccode;
CURSOR c3 IS
SELECT distinct("Product Code ")
FROM corder;
CURSOR c4(v_pcode number) IS
SELECT *
FROM corder
WHERE "Product Code "=v_pcode;
rs number;
TOTAL_PURCHASE number:=0;
TOTAL_SALE number:=0;
BEGIN
DELETE
FROM totalpurchase;
DELETE
FROM totalsale;
FOR i IN c1 LOOP
FOR j IN c2(i."Customer Code ") LOOP
SELECT decode(j."Product Code ",0,15,1,35,2,42,3,51,4,60,0) INTO rs
FROM dual;
rs:=rs*j."Quantity Sold ";
TOTAL_PURCHASE:=TOTAL_PURCHASE+rs;
END LOOP;
INSERT INTO totalpurchase
VALUES(i."Customer Code ",
TOTAL_PURCHASE);
END LOOP;
rs:=0;
FOR i IN c3 LOOP
FOR j IN c4(i."Product Code ") LOOP
SELECT decode(j."Product Code ",0,15,1,35,2,42,3,51,4,60,0) INTO rs
FROM dual;
rs:=rs*j."Quantity Sold ";
TOTAL_SALE:=TOTAL_SALE+rs;
END LOOP;
INSERT INTO totalsale
VALUES(i."Product Code ",
TOTAL_SALE);
END LOOP;
END;
/