Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 All Powder Board and Ski SQL Server Workbook Chapter 4: Queries Jerry Post Copyright © 2003.

Similar presentations


Presentation on theme: "1 All Powder Board and Ski SQL Server Workbook Chapter 4: Queries Jerry Post Copyright © 2003."— Presentation transcript:

1 1 All Powder Board and Ski SQL Server Workbook Chapter 4: Queries Jerry Post Copyright © 2003

2 2 Primary Tables

3 3 Action Copy files from BuildAllPowder. Drop or rename conflicting tables. Switch to command mode. Run the batch file BuildAllPowderSQL server username password Check the tables.

4 4 File/Get External Data/Import (1)Copy BuildAllPowder folder to machine running SQL Server (2)Drop or rename any conflicting tables (1)By hand (2)In SQL Plus, run the file DropAllPowderTablesSQL.sql (3)Switch to command mode: Start/All Programs/Accessories/Command Prompt (4)BuildAllPowderSQL server username password

5 5 All Powder Tables After Import

6 6 Starting a Query: Query Analyzer SQL statement Results Database connection Execute query Default database

7 7 Enterprise Manager View Results SQL text Tables and Joins Execute query Database

8 8 Action Start a new View. Right-click and Add the ItemModel table. Select columns: Category, ListPrice, WeightMax, Color, and Graphics. Enter conditions: Category=’Board’ AND ListPrice 150. Check the SQL text. Run the query.

9 9 Sample Query Display snowboards with a list price under $300 and max weight over 150 pounds. SQL Results Tables Constraints

10 10 More Complex Query Ski for jumping. Composite material. Red or Yellow main color. Yellow skis must be under $300. Red skis must be under $400 Three main conditions WHERE Category=‘Ski’ AND ItemMaterial=‘Composite’ AND Style=‘Jump’

11 11 Action Start a new query. Add the ItemModel table. SELECT Category, Color, ItemMaterial, Style, ListPrice Enter conditions: Category=’Ski’ And Style=’Jump’ And ItemMaterial=’Composite’ Run the query to ensure it works Add the conditions for Color=’Yellow’ and ListPrice<300 Test the query Add the conditions for Color=’Red’ and ListPrice<400 Add the correct parentheses Run the query and test it

12 12 Color Options Yellow and price conditions All 5 conditions must hold, so only one row matches

13 13 Multiple Conditions (Category=N’Ski’ AND Style=N’Jump’ AND ItemMaterial=N’Composite’) AND ( (Color=‘Yellow’ AND ListPrice<300) OR (Color=‘Red’ AND ListPrice<400) )

14 14 JOIN Query: Sales Sales in May Cash payment

15 15 Action Start with a blank query Add SELECT, FROM, WHERE Set SaleID, SaleDate, CustomerID, and PaymentMethod Use only the Sale table Set the SaleDate between 01-May-2004 AND 31-May- 2004 Set PaymentMethod to Cash Run the query to test it

16 16 JOIN Tables: Sale + Customer Join condition Additional columns

17 17 Action Add the Customer table Check the join condition: ON Sale.CustomerID = Customer.CustomerID Add Customer LastName and FirstName to the SELECT statement Run the query to test it

18 18 JOIN: SQL SELECT SaleID, SaleDate, Sale.CustomerID, LastName, FirstName, PaymentMethod FROM Sale INNER JOIN Customer ON Sale.CustomerID = Customer.CustomerID WHERE SaleDate Between ’01/05/2004' AND '31/05/2004' AND PaymentMethod='Cash'

19 19 Building a more complex query Which customers bought Atomic skis in January or February? What do you want to see?Customer names, SaleDate What do you know?Manufacturer name, SaleDate range, Category is Ski What tables are involved? How are they joined? Customer … Sale … ItemModel, Manufacturer SELECTLastName, FirstName, SaleDate FROM Customer, …, Sale, …, ItemModel, Manufacturer JOIN WHERE Manufacturer.Name=N‘Atomic”’ AND Sale.SaleDate BETWEEN ’01/01/2004’ AND ’29/02/2004’ AND ItemModel.Category = N‘Ski’

20 20 Join: Many Tables

21 21 SQL: Many Table JOINs SELECTLastName, FirstName, powder.ItemModel.Category, Name, SaleDate FROMpowder.Manufacturer INNER JOIN powder.ItemModel ON powder.Manufacturer.ManufacturerID = powder.ItemModel.ManufacturerID INNER JOIN powder.Inventory ON powder.ItemModel.ModelID = powder.Inventory.ModelID INNER JOIN powder.SaleItem ON powder.Inventory.SKU = powder.SaleItem.SKU INNER JOIN powder.Sale ON powder.SaleItem.SaleID = powder.Sale.SaleID INNER JOIN powder.Customer ON powder.Sale.CustomerID = powder.Customer.CustomerID WHERE(powder.ItemModel.Category = N'Ski') AND(Name = N'Atomic') AND(powder.Sale.SaleDate BETWEEN CONVERT(DATETIME, '2004-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2004-02-29 00:00:00', 102))

22 22 Older JOIN Method List tables separated by commas Place join condition in the WHERE clause

23 23 Calculations Calculated column

24 24 Action Create a new query using only the ItemModel table In the SELECT row, add a new pseudo column to compute ListPrice-Cost As Profit Add the ORDER BY line to sort by Category and List Price descending Run the query

25 25 Common Functions LowerTo lower case LenLength/number of characters SubstringGet substring LTrim, RTrimRemove leading and trailing spaces UpperTo upper case GetDateCurrent date DateAddAdd a number to a date DateDiffSubtract two dates ConvertHighly detailed formatting Day, Month, Year, DatePartGet parts of a date AbsAbsolute value CosCosine, all common trig functions FloorInteger, drop decimal values RoundRound-off Books Online: Transact-SQL Reference/Functions

26 26 Format Dates: Convert

27 27 Add Days and DateAdd SaleDate + one month SaleDate + 30 days

28 28 Action Create a new query Use only the Sale table SELECT SaleID and SaleDate Add 30 days to the SaleDate to get LateDate Use DateAdd to add one month to the SaleDate to get SaleMonth Run the query

29 29 Query: Sum Sum function

30 30 Action Create a new query Add the Sale table SELECT ShipState and SalesTax WHERE ShipState = ‘CA’ Run the query Verify the correct states are displayed Remove ShipState from SELECT SELECT Sum(SalesTax) AS SumOfSalesTax Run the query

31 31 SQL: Sum SELECT Sum(Sale.SalesTax) AS SumOfSalesTax FROM Sale WHERE Sale.ShipState=‘CA’

32 32 Query: Group By Group By produces subtotals for all values in the specified column

33 33 SQL: Group By SELECT ShipState, Sum(SalesTax) AS SumOfSalesTax FROM Sale GROUP BY ShipState;

34 34 Action Create a new query Use the Sale table Select columns: ShipState and Sum(SalesTax) AS SumOfSalesTax Click the Group By button and set Group By for the SaleState Run the query

35 35 Total Sales Value in Colorado Multiply on each row and sum across rows

36 36 CREATE VIEW—Save a Query CREATE VIEW ColoradoSales AS SELECT Sum(QuantitySold*SalePrice) AS SaleTotal FROM Sale INNER JOIN SaleItem ON Sale.SaleID = SaleItem.SaleID WHERE ShipState=’CO’


Download ppt "1 All Powder Board and Ski SQL Server Workbook Chapter 4: Queries Jerry Post Copyright © 2003."

Similar presentations


Ads by Google