Download presentation
Presentation is loading. Please wait.
Published byColin Maxwell Modified over 8 years ago
1
1 All Powder Board and Ski SQL Server 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 Action Create a new query Tables: Customer, Sale, SaleItem Columns: CustomerID, LastName, FirstName, Sum(QuantitySold*SalePrice) AS SalesValue Group By the other columns Run the query Save as a view CustomerSales Create new query Table: CustomerSales query SELECT Avg(SalesValue) … Run the query
5
5 Average Customer Sales
6
6 Action Create a new query Table: CustomerSales query Columns: LastName, FirstName, SalesValue Criteria for SalesValue >(SELECT Avg(SalesValue) FROM CustomerSales)
7
7 Best Customers Subquery
8
8 INNER JOIN: Sales and Rentals
9
9 Action Create a new query Tables: Rental and Sale Columns: RentDate, SaleDate, and CustomerID from both tables. Join the tables on CustomerID Run the query Add a join between the tables on Rental.CustomerID=Sale.CustomerID Run the query
10
10 Left Join: Sales + Rental Left Join Sale+Customer to Rental Sales without rentals
11
11 Action Create a new query FROM (Customer INNER JOIN Sale ON Customer.CustomerID=Sale.CustomerID) LEFT JOIN Rental ON Sale.CustomerID = Rental.CustomerID Columns: LastName, FirstName, and CustomerID from Sale and Rental Run the query
12
12 Inner Join: Same Customer and Day
13
13 Action Create a new query Tables: Customer and Sale Columns: LastName, FirstName, and CustomerID WHERE CustomerID Not In (SELECT CustomerID FROM Rental) Run the query
14
14 NOT IN Not In subquery to list those who did NOT rent
15
15 Action Create a new query Table: Inventory Columns: ModelID and Sum(QuantityOnHand) Sort by the Sum descending Run the query Save it as ModelsOnHand Create a new table: SalesCategory Columns: CategoryID, CategoryName, LowLimit, HighLimit Enter data from Figure 5.10
16
16 Model Quantity On Hand
17
17 Categories
18
18 Action Create a new query Columns: ModelID, SumOfQuantityOnHand, CategoryID, and CategoryName Tables: ModelsOnHand and SalesCategory Add the inequality join Run the query
19
19 Inequality Join Inequality (theta) joins assign the proper category name
20
20 Sales Categories Find the number of models in each sales category
21
21 Action Create a new query Columns: CustomerID, LastName, FirstName, and SaleDate Tables: Customer and Sale Set January sale date in WHERE Copy the entire statement Add the word Union Paste the SELECT statement and change the date condition and name to March Run the query
22
22 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.
23
23 Action Create a new query Enter the CREATE TABLE command Run the query
24
24 CREATE TABLE Query CREATE TABLE Contacts ( ContactIDint, ManufacturerIDint, LastNamenvarchar(25) null, FirstNamenvarchar(25) null, Phonenvarchar (15) null, Emailnvarchar (120) null, CONSTRAINT pk_Contacts PRIMARY KEY (ContactID), CONSTRAINT fk_ContactsManufacturer FOREIGN KEY (ManufacturerID) REFERENCES Manufacturer(ManufacturerID) ON DELETE CASCADE ) ;
25
25 Create a Temporary Table CREATE TABLE MyTemp ( IDint, LNamenvarchar(25), FNamenvarchar(25), CONSTRAINT pk_MyTemp PRIMARY KEY (ID) );
26
26 Action Create a new query Type the INSERT command: INSERT INTO Customer (CustomerID, LastName, FirstName, City, Gender) VALUES (4000, 'Jones', 'Jack', 'Nowhere', 'Male'); Run the query
27
27 INSERT INTO (One Row) INSERT INTO Customer (CustomerID, LastName, FirstName, City, Gender) VALUES (4000, 'Jones', 'Jack', 'Nowhere', 'Male'); Lab 7 shows how to create sequences so CustomerID is generated automatically.
28
28 INSERT INTO (Copy Rows) INSERT INTO MyTemp (ID, LName, FName) SELECT CustomerID, LastName, FirstName FROM Customer WHERE City='Sacramento' ;
29
29 Action Create a new query Columns: Category, ModelYear, and Round(Cost*1.04,2) Table: ItemModel Criteria: Category=’Board’ And ModelYear=2004 Run the query Change the first two lines to be: UPDATE ItemModel SET Cost = Round(Cost*1.04,2) Run the query
30
30 UPDATE Board Cost Test query to see new values. Delete it before running second query Query to change data in the Cost column
31
31 Action Create a new SELECT query Columns: ID, LName, FName Table: MyTemp Criteria: ID>100 Test the query Change the SELECT row to DELETE Run the query Run a commit; command
32
32 DELETE Rows First write a SELECT query to see the rows Second change the SELECT row to DELETE
33
33 DROP TABLE DROP TABLE MyTemp;
34
34 Action Create a new query Columns: Category, Sum(RentFee) Tables: Rental, RentItem, Inventory, and ItemModel GROUP BY Category Test the query
35
35 Query Parameters SELECT Category, Sum(RentFee) AS SumOfRentFee FROM Rental INNER JOIN RentItem ON Rental.RentID=RentItem.RentID INNER JOIN Inventory ON RentItem.SKU=Inventory.SKU INNER JOIN ItemModel ON Inventory.ModelID=ItemModel.ModelID 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
36
36 Action Create a new query Copy or paste the code to create the package and procedure Run the package creation code Enter the five commands to execute the parameter query Run the query Change the dates to Oct-Dec Run the query
37
37 Query Parameters: Procedure CREATE PROCEDURE GetCategoryFees @StartDate datetime, @EndDate datetime AS SELECT Category, Sum(RentFee) AS SumOfRentFee FROM Rental INNER JOIN RentItem ON Rental.RentID=RentItem.RentID INNER JOIN Inventory ON RentItem.SKU=Inventory.SKU INNER JOIN ItemModel ON Inventory.ModelID=ItemModel.ModelID WHERE RentDate Between @StartDate And @EndDate GROUP BY Category GO
38
38 Query Parameters Parameters: specify in order Or specify by name
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.