Chapter 4

Post on 06-Jan-2016

23 views 0 download

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

1

Chapter 4

Query Examples for S&S

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

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

4

Relationship map

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

6

Relationship Map

Select create tabThen select Query Design

7

Highlight needed tables and select add

8

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

9

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

run the query

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"));

11

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

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

13

Query 2 Dynaset

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

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)

16

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

17

•Select total symbol•The total line will appear

•Select option from drop down menu

18

You may want to give “SumOfQuantity a more meaningful name

19

Place label in front of attributeUnit Sales: Quantity or

20

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

21

Query 2 Dynaset

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

23

Query 4 Dynaset

24

Table 4-5

Data for this query contained in 3 tables

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

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

PartA

26

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

27

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

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

29

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

PartB

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;

31

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

32

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

33