Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 All Powder Board and Ski Microsoft Access Workbook Chapter 5: Advanced Queries Jerry Post Copyright © 2007.

Similar presentations


Presentation on theme: "1 All Powder Board and Ski Microsoft Access Workbook Chapter 5: Advanced Queries Jerry Post Copyright © 2007."— Presentation transcript:

1 1 All Powder Board and Ski Microsoft Access Workbook Chapter 5: Advanced Queries Jerry Post Copyright © 2007

2 2 Primary Tables

3 3 Find Best Customers: 1 Total sales by customer.

4 4 Action Create a new query in Design view Tables: Customer, Sale, SaleItem Columns: CustomerID, LastName, FirstName, SalesValue: [QuantitySold]*[SalePrice] Sum the SalesValue, Group By the rest Save query as CustomerSales Create new query in Design view Table: CustomerSales query Columns: SalesValue Set Totals and select Avg Run the query

5 5 Average Customer Sales SELECT Avg(CustomerSales.SalesValue) AS AvgOfSalesValue FROM CustomerSales; $942.11

6 6 Action Create a new query in Design view Table: CustomerSales query Columns: LastName, FirstName, SalesValue Criteria for SalesValue (enter in SQL) >(SELECT Avg(SalesValue) FROM CustomerSales)

7 7 Best Customers SELECT.LastName, FirstName, SalesValue FROM CustomerSales WHERE SalesValue > (Select Avg(SalesValue) FROM CustomerSales) ORDER BY SalesValue DESC;

8 8 INNER JOIN: Sales and Rentals SELECT Rental.RentDate, Rental.CustomerID, Sale.CustomerID, Sale.SaleDate FROM Rental INNER JOIN Sale ON Rental.CustomerID = Sale.CustomerID;

9 9 Action Create a new query in Design view Tables: Rental and Sale Columns: RentDate, SaleDate, and CustomerID from both tables. Join the tables on CustomerID Run the query Join the tables on RentDate=SaleDate Run the query

10 10 Inner Join: Same Customer and Day SELECT Rental.RentDate, Rental.CustomerID, Sale.CustomerID, Sale.SaleDate FROM Rental INNER JOIN Sale ON (Rental.RentDate = Sale.SaleDate) AND (Rental.CustomerID = Sale.CustomerID);

11 11 Action Create a new query in Design view Tables: Customer, Sale, Rental Columns: LastName, FirstName, and CustomerID from Sale and Rental Delete join from Rental to Customer Add join from Sale to Rental Double click this new join Select option to include all from Sale Run the query

12 12 LEFT JOIN: Sales + Rental SELECT Customer.LastName, Customer.FirstName, Sale.CustomerID, Rental.CustomerID FROM (Customer INNER JOIN Sale ON Customer.CustomerID = Sale.CustomerID) LEFT JOIN Rental ON Sale.CustomerID = Rental.CustomerID; Join Properties: Left Join

13 13 LEFT JOIN Results Customers who purchased items without renting anything have missing (Null) values for the Rental.CustomerID

14 14 Action Create a new query in Design view Tables: Customer and Sale Columns: LastName, FirstName, and CustomerID Criteria for CustomerID (use SQL): Not In (SELECT CustomerID FROM Rental) Check the SQL Run the query

15 15 NOT IN SELECT Customer.LastName, Customer.FirstName, Customer.CustomerID FROM Customer INNER JOIN Sale ON Customer.CustomerID = Sale.CustomerID WHERE Customer.CustomerID Not In (SELECT CustomerID FROM Rental) ORDER BY Customer.LastName, Customer.FirstName;

16 16 Action Create a new query in Design view Table: Inventory Columns: ModelID and QuantityOnHand Sum the QuantityOnHand and sort it in descending order Run the query Save it as ModelsOnHand Create a new table in Design view Columns: CategoryID, CategoryName, LowLimit, HighLimit Save it as SalesCategory Enter data

17 17 Model Quantity On Hand

18 18 Categories

19 19 Action Create a new query in Design view Tables: ModelsOnHand and SalesCategory Columns: ModelID, SumOfQuantityOnHand, CategoryID, and CategoryName In SQL view add the inequality join Run the query

20 20 Inequality Join SELECT ModelsOnHand.ModelID, ModelsOnHand.SumOfQuantityOnHand, SalesCategory.CategoryID, SalesCategory.CategoryName FROM ModelsOnHand INNER JOIN SalesCategory ON (ModelsOnHand.SumOfQuantityOnHand>=SalesCategory.LowLimit) AND (ModelsOnHand.SumOfQuantityOnHand<SalesCategory.HighLimit); The join cannot be displayed in design view and must be entered by hand in SQL view.

21 21 Sales Categories

22 22 Action Create a new query in Design view Tables: Customer and Sale Columns: CustomerID, LastName, FirstName, and SaleDate Set January sale date in criteria row Switch to SQL Copy the entire statement Add the word Union Paste the SELECT statement and change the date condition to March Run the query

23 23 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. SELECT Customer.CustomerID, LastName, FirstName, "Jan" As SaleMonth FROM Customer INNER JOIN Sale ON Customer.CustomerID = Sale.CustomerID WHERE (((Sale.SaleDate) Between #1/1/2004# And #1/31/2004#)) UNION SELECT Customer.CustomerID, LastName, FirstName, "Mar" As SaleMonth FROM Customer INNER JOIN Sale ON Customer.CustomerID = Sale.CustomerID WHERE (((Sale.SaleDate) Between #3/1/2004# And #3/31/2004#));

24 24 Action Create a new query in Design view Tables: Rental, RentItem, Inventory, and ItemModel Columns: RentDate, Category, RentFee Set totals to sum RentFee Set Where and criteria for RentDate to Between [Start Date] And [End Date] Run the query

25 25 Query Parameters

26 26 Action Create a new query in Design view Do not select any tables Switch to SQL view Enter the CREATE TABLE command Run the query

27 27 CREATE TABLE Query CREATE TABLE Contacts ( ContactIDLong, ManufacturerIDLong, LastNameText(25), FirstNameText(25), PhoneText(15), EmailText(120), CONSTRAINT pk_Contacts PRIMARY KEY (ContactID), CONSTRAINT fk_ContactsManufacturer FOREIGN KEY (ManufacturerID) REFERENCES Manufacturer(ManufacturerID) ) ;

28 28 Create a Temporary Table CREATE TABLE MyTemp ( IDLong, LNameText(25), FNameText(25) );

29 29 Action Create a new query in Design view Do not select any tables Switch to SQL view Type the INSERT command: INSERT INTO Customer (LastName, FirstName, City, Gender) VALUES ('Jones', 'Jack', 'Nowhere', 'Male'); Run the query

30 30 INSERT INTO (One Row) INSERT INTO Customer (LastName, FirstName, City, Gender) VALUES ('Jones', 'Jack', 'Nowhere', 'Male');

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

32 32 Action Create a new query in Design view Table: ItemModel Columns: Category, ModelYear, and Cost Criteria: Category=Board And ModelYear=2004 Run the query Choose Query/Update Query Under Cost set Update To: Round([Cost]*1.04,2) Run the query

33 33 UPDATE Board Cost Query / Update Query New value UPDATE ItemModel SET Cost = Round([Cost]*1.04,2) WHERE (Category="Board") AND (ModelYear=2004); Run

34 34 Action Create a new query in Design view Table: MyTemp Columns: ID, LName, FName Criteria: ID>100 Test the query Choose Query/Delete Query Run the query

35 35 DELETE Rows DELETE FROM MyTemp WHERE ID > 100;

36 36 DROP TABLE DROP TABLE MyTemp;


Download ppt "1 All Powder Board and Ski Microsoft Access Workbook Chapter 5: Advanced Queries Jerry Post Copyright © 2007."

Similar presentations


Ads by Google