1 All Powder Board and Ski Microsoft Access Workbook Chapter 4: Queries Jerry Post Copyright © 2003
2 Primary Tables
3 File/Get External Data/Import The sample files include column names Comma delimited is common Make sure columns are correct
4 Starting a Query Select tables Show table list Display fields/columns Conditions Switch to SQL or view results
5 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));
6 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
7 Color Options Yellow and price conditions All 5 conditions must hold, so only one row matches
8 Multiple Conditions Add a new Criteria row for red skis
9 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) );
10 JOIN Query: Sales Sales in MayCash payment
11 JOIN Tables: Sale + Customer Matching names
12 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");
13 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”
14 Join: Many Tables
15 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#));
16 Calculations Calculated column SELECT Category, ItemMaterial, ListPrice, ListPrice-Cost AS Profit FROM ItemModel ORDER BY Category, ListPrice DESC;
17 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
18 Format Function
19 DateAdd and DateDiff SaleDate + one monthSaleDate + 30 days
20 Query: Sum Totals button adds the Total row Calculation functions SumOfSalesTax $5,332.11
21 SQL: Sum SELECT Sum(Sale.SalesTax) AS SumOfSalesTax FROM Sale WHERE Sale.ShipState="CA"
22 Query: Group By Group By produces subtotals for all values in the specified column
23 SQL: Group By SELECT Sale.ShipState, Sum(Sale.SalesTax) AS SumOfSalesTax FROM Sale GROUP BY Sale.ShipState;
24 Total Sales Value in Colorado SaleTotal $4, SELECT Sum([QuantitySold]*[SalePrice]) AS SaleTotal FROM Sale INNER JOIN SaleItem ON Sale.SaleID = SaleItem.SaleID WHERE Sale.ShipState="CO";