Download presentation
Presentation is loading. Please wait.
Published byOswald Richardson Modified over 9 years ago
1
1 All Powder Board and Ski Microsoft Access Workbook Chapter 4: Queries Jerry Post Copyright © 2007
2
2 Primary Tables
3
3 Action Choose File/Get External Data/Import Browse to the appropriate csv file Set comma delimited Check the box to include column names If your table matches the file, check the box to read into an existing table
4
4 File/Get External Data/Import The sample files include column names Comma delimited is common Make sure columns are correct
5
5 Starting a Query Select tables Show table list Display fields/columns Conditions Switch to SQL or view results
6
6 Action Create a new query in Design view Add the ItemModel table Select columns for Category, ListPrice, WeightMax, Color, and Graphics Enter conditions for Board, ListPrice, and Weight Run the query
7
7 Sample Query Display snowboards with a list price under $300 and max weight over 150 pounds. SELECT ItemModel.Category, ItemModel.ListPrice, ItemModel.WeightMax, ItemModel.Color, ItemModel.Graphics FROM ItemModel WHERE (((ItemModel.Category)="Board") AND ((ItemModel.ListPrice) 150));
8
8 Action Create a new query in Design view Add the ItemModel table Add columns: Category, Color, ItemMaterial, Style, and ListPrice Set requested conditions Check the SQL Run the query
9
9 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
10
10 Color Options Yellow and price conditions All 5 conditions must hold, so only one row matches
11
11 Multiple Conditions Add a new Criteria row for red skis
12
12 SQL Versions SELECT ItemModel.Category, ItemModel.Color, ItemModel.ItemMaterial, ItemModel.Style, ItemModel.ListPrice FROM ItemModel WHERE (((ItemModel.Category)="Ski") AND ((ItemModel.Color)="Yellow") AND ((ItemModel.ItemMaterial)="Composite") AND ((ItemModel.Style)="Jump") AND ((ItemModel.ListPrice)<300)) OR (((ItemModel.Category)="Ski") AND ((ItemModel.Color)="Red") AND ((ItemModel.ItemMaterial)="Composite") AND ((ItemModel.Style)="Jump") AND ((ItemModel.ListPrice)<400)); SELECT Category, Color, ItemMaterial, Style, ListPrice FROM ItemModel WHERE (Category="Ski" AND ItemMaterial="Composite" AND Style="Jump") AND ( ( Color="Yellow" AND ListPrice<300) OR (Color="Red" AND ListPrice<400) );
13
13 JOIN Query: Sales Sales in MayCash payment
14
14 Action Create a new query in Design view Choose the Sale table Select columns: SaleID, SaleDate, CustomerID, and PaymentMethod Set conditions for Cash sales in May Choose Query/Show Table (or button) Add the Customer table Add columns: LastName, FirstName View the SQL Run the query
15
15 JOIN Tables: Sale + Customer Matching names
16
16 JOIN: SQL SELECT Sale.SaleID, Sale.SaleDate, Sale.CustomerID, Customer.LastName, Customer.FirstName, Sale.PaymentMethod FROM Customer INNER JOIN Sale ON Customer.CustomerID = Sale.CustomerID WHERE (Sale.SaleDate Between #5/1/2004# And #5/31/2004# AND Sale.PaymentMethod="Cash");
17
17 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 1/1/2004 AND 2/29/2004 AND ItemModel.Category = “Ski”
18
18 Join: Many Tables
19
19 SQL: Many Table Joins SELECT Customer.LastName, Customer.FirstName, ItemModel.Category, Manufacturer.Name, Sale.SaleDate FROM Manufacturer INNER JOIN (ItemModel INNER JOIN (Inventory INNER JOIN ((Customer INNER JOIN Sale ON Customer.CustomerID = Sale.CustomerID) INNER JOIN SaleItem ON Sale.SaleID = SaleItem.SaleID) ON Inventory.SKU = SaleItem.SKU) ON ItemModel.ModelID = Inventory.ModelID) ON Manufacturer.ManufacturerID = ItemModel.ManufacturerID WHERE (((ItemModel.Category)="Ski") AND ((Manufacturer.Name)="Atomic") AND ((Sale.SaleDate) Between #1/1/2004# And #2/29/2004#));
20
20 Action Create a new query in Design view Add the ItemModel table Select columns: Category, ItemMaterial, and ListPrice Create new column as Profit: [ListPrice]-[Cost] Run the query
21
21 Calculations Calculated column SELECT Category, ItemMaterial, ListPrice, ListPrice-Cost AS Profit FROM ItemModel ORDER BY Category, ListPrice DESC;
22
22 Common Functions LcaseTo lower case LenLength/number of characters MidGet substring TrimRemove leading and trailing spaces UcaseTo upper case DateCurrent date DateAddAdd days, months, years to a date DateDiffSubtract two dates FormatHighly detailed formatting NowCurrent date and time AbsAbsolute value CosCosine, all common trig functions IntInteger, drop decimal values SgnSignum RoundRound-off
23
23 Format Function
24
24 Action Create a new query in Design view Add the Sale table Select columns: SaleID and SaleDate Create new column as LateDate: [SaleDate]+30 Another new column as LateMonth: DateAdd(“m”,1,[SaleDate]) Run the query
25
25 DateAdd and DateDiff SaleDate + one monthSaleDate + 30 days
26
26 Action Create a new query in Design view Add the Sale table Select columns: ShipState and SalesTax View/Totals (or Totals button) Select “Where” for Total row in State Enter “CA” as a criteria Select “Sum” for SalesTax Total row Run the query
27
27 Query: Sum Totals button adds the Total row Calculation functions SumOfSalesTax $5,332.11
28
28 SQL: Sum SELECT Sum(Sale.SalesTax) AS SumOfSalesTax FROM Sale WHERE Sale.ShipState="CA"
29
29 Action Create a new query in Design view Add the Sale table Select columns: ShipState and SalesTax View/Totals (or Totals button) Select “Sum” for SalesTax Total row Run the query
30
30 Query: Group By Group By produces subtotals for all values in the specified column
31
31 SQL: Group By SELECT Sale.ShipState, Sum(Sale.SalesTax) AS SumOfSalesTax FROM Sale GROUP BY Sale.ShipState;
32
32 Total Sales Value in Colorado SaleTotal $4,964.00 SELECT Sum([QuantitySold]*[SalePrice]) AS SaleTotal FROM Sale INNER JOIN SaleItem ON Sale.SaleID = SaleItem.SaleID WHERE Sale.ShipState="CO";
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.