Download presentation
Presentation is loading. Please wait.
Published byMarion Briggs Modified over 8 years ago
1
1 All Powder Board and Ski Microsoft Access Workbook Chapter 5: Advanced Queries Jerry Post Copyright © 2003
2
2 Primary Tables
3
3 Find Best Customers: 1 Total sales by customer.
4
4 Average Customer Sales SELECT Avg(CustomerSales.SalesValue) AS AvgOfSalesValue FROM CustomerSales; $942.11
5
5 Best Customers SELECT.LastName, FirstName, SalesValue FROM CustomerSales WHERE SalesValue > (Select Avg(SalesValue) FROM CustomerSales) ORDER BY SalesValue DESC;
6
6 INNER JOIN: Sales and Rentals SELECT Rental.RentDate, Rental.CustomerID, Sale.CustomerID, Sale.SaleDate FROM Rental INNER JOIN Sale ON Rental.CustomerID = Sale.CustomerID;
7
7 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);
8
8 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
9
9 Left Join Results Customers who purchased items without renting anything have missing (Null) values for the Rental.CustomerID
10
10 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;
11
11 Model Quantity On Hand
12
12 Categories
13
13 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.
14
14 Sales Categories
15
15 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#));
16
16 Query Parameters
17
17 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) ) ;
18
18 Create a Temporary Table CREATE TABLE MyTemp ( IDLong, LNameText(25), FNameText(25) );
19
19 INSERT INTO (One Row) INSERT INTO Customer (LastName, FirstName, City, Gender) VALUES ('Jones', 'Jack', 'Nowhere', 'Male');
20
20 INSERT INTO (Copy Rows) INSERT INTO MyTemp (ID, LName, FName) SELECT CustomerID, LastName, FirstName FROM Customer WHERE City='Sacramento' ;
21
21 UPDATE Board Cost Query / Update Query New value UPDATE ItemModel SET Cost = Round([Cost]*1.04,2) WHERE (Category="Board") AND (ModelYear=2004); Run
22
22 DELETE Rows DELETE FROM MyTemp WHERE ID > 100;
23
23 DROP TABLE DROP TABLE MyTemp;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.