Presentation is loading. Please wait.

Presentation is loading. Please wait.

All Powder Board and Ski

Similar presentations


Presentation on theme: "All Powder Board and Ski"— Presentation transcript:

1 All Powder Board and Ski
Oracle 9i Workbook Chapter 5: Advanced Queries Jerry Post Copyright © 2003

2 Primary Tables

3 Find Best Customers: 1 Total sales by customer.

4 Average Customer Sales

5 Best Customers Subquery

6 INNER JOIN: Sales and Rentals

7 Left Join: Sales + Rental
Left Join Sale+Customer to Rental Sales without rentals

8 Inner Join: Same Customer and Day

9 NOT IN Not In subquery to list those who did NOT rent

10 Model Quantity On Hand

11 Categories

12 Inequality Join Inequality (theta) joins assign the proper category name

13 Sales Categories Find the number of models in each sales category

14 UNION Query List customers who bought items in January or in March.
Note: it could be done with simple conditions, but it is good practice for UNION.

15 CREATE TABLE Query CREATE TABLE Contacts ( ContactID INTEGER,
ManufacturerID INTEGER, LastName NVARCHAR2(25), FirstName NVARCHAR2(25), Phone NVARCHAR2 (15), NVARCHAR2 (120), CONSTRAINT pk_Contacts PRIMARY KEY (ContactID), CONSTRAINT fk_ContactsManufacturer FOREIGN KEY (ManufacturerID) REFERENCES Manufacturer(ManufacturerID) ) ;

16 Create a Temporary Table
CREATE TABLE MyTemp ( ID INTEGER, LName NVARCHAR2(25), FName NVARCHAR2(25), CONSTRAINT pk_MyTemp PRIMARY KEY (ID) );

17 INSERT INTO (One Row) INSERT INTO Customer (CstomerID, LastName, FirstName, City, Gender) VALUES (4000, 'Jones', 'Jack', 'Nowhere', 'Male'); Lab 7 shows how to create sequences so CustomerID is generated automatically.

18 INSERT INTO (Copy Rows)
INSERT INTO MyTemp (ID, LName, FName) SELECT CustomerID, LastName, FirstName FROM Customer WHERE City='Sacramento' ;

19 UPDATE Board Cost Test query to see new values.
Delete it before running second query Query to change data in the Cost column

20 DELETE Rows First write a SELECT query to see the rows
Second change the SELECT row to DELETE Execute the Commit; statement to permanently save changes

21 DROP TABLE DROP TABLE MyTemp;

22 Query Parameters SELECT Category, Sum(RentFee) AS SumOfRentFee
FROM Rental INNER JOIN RentItem INNER JOIN Inventory INNER JOIN ItemModel ON Inventory.ModelID=ItemModel.ModelID ON RentItem.SKU=Inventory.SKU ON Rental.RentID=RentItem.RentID WHERE RentDate Between ’01-Jan-2004’ And ’31-Mar-2004’ GROUP BY Category; Need to replace fixed dates with parameters that can be entered by a manager

23 Query Parameters: Package
CREATE PACKAGE pckCategoryFees AS TYPE typeCategoryFees IS RECORD ( Category NVARCHAR2(15), SumOfRentFees NUMBER(8,2) ); TYPE typeCursorFees IS REF CURSOR RETURN typeCategoryFees; PROCEDURE GetCategoryFees ( dateStart IN DATE, dateEnd IN DATE, cvFees IN OUT typeCursorFees END; /

24 Package Body CREATE PACKAGE BODY pckCategoryFees AS
PROCEDURE GetCategoryFees ( dateStart IN DATE, dateEnd IN DATE, cvFees IN OUT typeCursorFees ) IS BEGIN OPEN cvFees FOR SELECT Category, Sum(RentFee) AS SumOfRentFee FROM Rental INNER JOIN RentItem INNER JOIN Inventory INNER JOIN ItemModel ON Inventory.ModelID=ItemModel.ModelID ON RentItem.SKU=Inventory.SKU ON Rental.RentID=RentItem.RentID WHERE RentDate Between dateStart And dateEnd GROUP BY Category; END;

25 Query Parameters Parameters: dateStart and dateEnd


Download ppt "All Powder Board and Ski"

Similar presentations


Ads by Google