Download presentation
Presentation is loading. Please wait.
Published byPiers Rogers Modified over 8 years ago
1
1 All Powder Board and Ski Oracle 9i Workbook Chapter 4: Queries Jerry Post Copyright © 2003
2
2 Primary Tables
3
3 File/Get External Data/Import (1)Copy BuildAllPowder folder to machine running SQL Plus (2)Drop or rename any conflicting tables (1)By hand (2)In SQL Plus, run the file DropAllPowserTablesOracle.sql (3)Edit BuildAllPowderOracle.sql in Wordpad and set the directory for the CSV files (4)Start SQL Plus (not the worksheet) (5)@@BuildAllPowderOracle.sql create or replace directory csv_dir as 'd:\students\BuildAllPowder\AllPowderSampleDataCSV';
4
4 All Powder Tables after Import
5
5 Starting a Query: SQL Worksheet SQL statement Results Database connection Execute query Prior commands
6
6 Sample Query Display snowboards with a list price under $300 and max weight over 150 pounds. SQL Results
7
7 Formatting Columns Set width Format currency
8
8 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’
9
9 Color Options Yellow and price conditions All 5 conditions must hold, so only one row matches
10
10 Multiple Conditions ( (Color=‘Yellow’ AND ListPrice<300) OR (Color=‘Red’ AND ListPrice<400) )
11
11 JOIN Query: Sales Sales in May Cash payment
12
12 JOIN Tables: Sale + Customer Join condition Additional columns
13
13 JOIN: SQL COLUMN PaymentMethod Format A15; COLUMN LastName Format A15; COLUMN FirstNAme Format A15; SELECT SaleID, SaleDate, Sale.CustomerID, LastName, FirstName, PaymentMethod FROM Sale INNER JOIN Customer ON Sale.CustomerID = Customer.CustomerID WHERE SaleDate Between '01-May-2004' AND '31-May-2004' AND PaymentMethod='Cash' ;
14
14 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=‘Atomic”’ AND Sale.SaleDate BETWEEN ‘01-Jan-2004’ AND ‘29-Feb-2004’ AND ItemModel.Category = ‘Ski’
15
15 Join: Many Tables
16
16 SQL: Many Table Joins COLUMN LastName Format A15; COLUMN FirstName Format A15; COLUMN Category Format A15; SELECT LastName, FirstName, Category, Name, SaleDate FROM Manufacturer INNER JOIN ItemModel INNER JOIN Inventory INNER JOIN SaleItem INNER JOIN Sale INNER JOIN Customer ON Sale.CustomerID = Customer.CustomerID ON SaleItem.SaleID = Sale.SaleID ON Inventory.SKU = SaleItem.SKU ON ItemModel.ModelID = Inventory.ModelID ON Manufacturer.ManufacturerID = ItemModel.ManufacturerID WHERE Category = 'Ski' AND Name='Atomic' AND SaleDate BETWEEN '01-Jan-2004' AND '29-Feb-2004' ;
17
17 Older Join Method List tables separated by commas Place join condition in the WHERE clause
18
18 Calculations Calculated column
19
19 Common Functions LowerTo lower case LengthLength/number of characters SubstrGet substring TrimRemove leading and trailing spaces UpperTo upper case SYSDATECurrent date ADD_MONTHSAdd days, months, years to a date MONTHS_BETWEENSubtract two dates TO_CHARHighly detailed formatting TO_DATEFormat dates SYSDATECurrent date and time AbsAbsolute value CosCosine, all common trig functions FloorInteger, drop decimal values RoundRound-off
20
20 Format Dates: TO_CHAR
21
21 DateAdd and DateDiff SaleDate + one month SaleDate + 30 days
22
22 Query: Sum Sum function
23
23 SQL: Sum SELECT Sum(Sale.SalesTax) AS SumOfSalesTax FROM Sale WHERE Sale.ShipState=‘CA’
24
24 Query: Group By Group By produces subtotals for all values in the specified column
25
25 SQL: Group By SELECT ShipState, Sum(SalesTax) AS SumOfSalesTax FROM Sale GROUP BY ShipState;
26
26 Total Sales Value in Colorado Multiply on each row and sum across rows
27
27 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’
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.