Chapter 4

33
Chapter 4 Query Examples for S&S 1

description

Chapter 4. Query Examples for S&S. Query 1. Sales events to D.Ainge and Sales Rep associated with event Key to querying a database Identify the tables/queries that have the data that you need Include these tables/queries in your query design - PowerPoint PPT Presentation

Transcript of Chapter 4

Page 1: Chapter  4

1

Chapter 4

Query Examples for S&S

Page 2: Chapter  4

2

Query 1• Sales events to D.Ainge and Sales Rep

associated with event• Key to querying a database

• Identify the tables/queries that have the data that you need

• Include these tables/queries in your query design

• If needed, establish the necessary joins between the data sources

• Do not include extra tables

Page 3: Chapter  4

3

Table 4-5

Data for this query contained in 2 tables

• Sales table• Customer table

Sales TableSales Invoice # Date Salesperson Customer #

101 15-Oct-18 J. Buck 151102 15-Oct-18 S. Knight 152103 28-Oct-18 S. Knight 151104 31-Oct-18 J. Buck 152105 14-Nov-18 J. Buck 153

Page 4: Chapter  4

4

Relationship map

Page 5: Chapter  4

5

Relationship map using VisioCustomer

PK Customer #

Customer Name Street City State

Inventory

PK Item #

Unit Price Description

Sales

PK Sales Invoice #

Date SalespersonFK1 Customer #

Sales-Inventory

PK,FK2 Sales Invoice #PK,FK1 Item #

Quantity

Page 6: Chapter  4

6

Relationship Map

Select create tabThen select Query Design

Page 7: Chapter  4

7

Highlight needed tables and select add

Page 8: Chapter  4

8

Double click or highlight and drag each attribute you need to add to the table below

Page 9: Chapter  4

9

•Enter your selection criteria•Text criteria needs to be in quotesWhen finished

run the query

Page 10: Chapter  4

10

Resulting Dynaset

SQL code generated by Access for this query

SELECT Sales.[Sales Invoice #], Sales.Salesperson, Customer.[Customer Name]FROM Customer INNER JOIN Sales ON Customer.[Customer #]=Sales.[Customer #]WHERE (((Customer.[Customer Name])="d. ainge"));

Page 11: Chapter  4

11

Close out queryYou will be prompted to saveIf you want to save it, select yes and give the query a name

Page 12: Chapter  4

12

Query 2 (modified)

• Query will calculated unit sales by item for the month of October 2018

• Query will require use of the summation aggregation function

• Query will use the group by feature• Query will use date selection criteria

Page 13: Chapter  4

13

Query 2 Dynaset

Page 14: Chapter  4

Aggregation Functions in Queries

• An aggregation function summarizes the data values within a field (column)• COUNT summarizes the number of rows that

contain a given value in the field• AVERAGE computes the arithmetic mean value

of all rows included in the answer• SUM computes the arithmetic sum of all rows

included in the answer• MIN identifies the minimum (lowest) attribute

value for the field• MAX identifies the maximum (greatest) attribute

value for the field14

Page 15: Chapter  4

15

Sales TableSales Invoice # Date Salesperson Customer #

101 15-Oct-18 J. Buck 151102 15-Oct-18 S. Knight 152103 28-Oct-18 S. Knight 151104 31-Oct-18 J. Buck 152105 14-Nov-18 J. Buck 153

Table 4-5

Data for this query contained in 3 tables

• Sales table (date)• Sales - Inventory table (quantity)• Inventory table (description)

Page 16: Chapter  4

16

Date criteriaBetween #10/1/2018# And #10/31/2018#Note: do not check show date for this query

Page 17: Chapter  4

17

•Select total symbol•The total line will appear

•Select option from drop down menu

Page 18: Chapter  4

18

You may want to give “SumOfQuantity a more meaningful name

Page 19: Chapter  4

19

Place label in front of attributeUnit Sales: Quantity or

Page 20: Chapter  4

20

Or right click on quantity field to bring up property sheetEnter caption

Page 21: Chapter  4

21

Query 2 Dynaset

Page 22: Chapter  4

22

Query 4 (modified)• Query will calculate total for each October

sales invoice • Book example uses 1 query

• Modified Query will be done in two parts• Part A will do the necessary horizontal

calculations for line item extension• Horizontal” calculations mathematically combine

values from different fields for each row• Creates a calculated field in the query

• Final part will use part A query to calculate the totals using summation aggregation function

Page 23: Chapter  4

23

Query 4 Dynaset

Page 24: Chapter  4

24

Table 4-5

Data for this query contained in 3 tables

• Sales table (date & invoice #)• Sales - Inventory table (quantity)• Inventory table (Unit Price)

Page 25: Chapter  4

Line item extension: [unit price]*[quantity]Syntax for Formula important, must use [ ]Typos in attribute name will result in error message

PartA

Page 26: Chapter  4

26

Syntax error example:Line item extension: (unit price]*[quantity]

Page 27: Chapter  4

27

Typo in attribute name example:Line item extension: [unit price]*[quantty]

Page 28: Chapter  4

28

SQL code generated by Access

SELECT Sales.Date, Sales.[Sales Invoice #], [unit price]*[quantity] AS [Line Item Extension]

FROM Sales INNER JOIN (Inventory INNER JOIN [Sales-Inventory] ON Inventory.[Item #] = [Sales-Inventory].[Item #]) ON Sales.[Sales Invoice #] = [Sales-Inventory].[Sales Invoice #]

WHERE (((Sales.Date) Between #10/1/2018# And #10/31/2018#));

PartA

dynaset

Page 29: Chapter  4

29

Line item extension query is input for final query to get invoice total

PartB

Page 30: Chapter  4

30

Query 4 Dynaset

SQL code generated by Access

SELECT [Qry Lime Item part A].Date, [Qry Lime Item part A].[Sales Invoice #], Sum([Qry Lime Item part A].[Line Item Extension]) AS [Invoice Total]

FROM [Qry Lime Item part A]

GROUP BY [Qry Lime Item part A].Date, [Qry Lime Item part A].[Sales Invoice #]

ORDER BY Sum([Qry Lime Item part A].[Line Item Extension]) DESC;

Page 31: Chapter  4

31

If you want Invoice Total field to be in currency format, right click in the field column and select properties

Page 32: Chapter  4

32

In property sheet box, select the format you want for the calculated field

Page 33: Chapter  4

33