2
Examples of Needs for Multiple Views of One Data Set
• Cash-basis versus Accrual Accounting• Weighted Average versus FIFO or LIFO• Double-Declining Balance Depreciation
versus Straight Line• Foreign Currency Translation
How do we get these multiple views???????
3
Answer: Query the Data Set
• What is Querying?– It is asking questions about the data in t
he database and manipulating or combining the data in different ways
– We can isolate certain rows in tables, we can isolate certain columns in tables, we can join tables together, we can create calculations based on various data items, etc.
4
Generating Reports• Ad-hoc reports
– The user can easily manipulate the base level tables to generate information on an ad hoc (as needed) basis.
– This is the most effective way to provide data availability to users of the database when their information needs change on an ongoing basis.
• Regular Reports– We can actually create views of the data that look like additional tabl
es, but are just alternative ways to view the data that already exists in the database.
– The data are not copied to a second physical location in the database.
– Instead, a view creates the appearance of a different set of tables for the user in the format the user wants to see.
– The Schema for the Client Billing and Human Resources Portion of the Database is provided in Figure 6.17
– Creating a View of the Client Billing Detail with SQL is provided in Figure 6.18
5
Three Query Languages
• Relational Algebra– Three main operators: Select, Project, Join– Provides the conceptual basis for SQL and QBE
• Structured Query Language (SQL)– The user enters commands according to a pre-defined s
yntax to retrieve desired data. • Query By Example (QBE)
– The user starts with a sample of the table(s) columns and marks the fields he or she wants to include in the answer. Defaults are available for summarizing and manipulating the data.
6
Relational Algebra
• Select– includes only certain rows from a database
table in its “answer”. • Project
– includes only certain columns from a database table in its “answer”
• Join– combines two or more database tables on t
he basis of one or more common attributes
7
Example Tables (Incomplete Enterprise Database)from Dunn & McCarthy (2004) working paper
Inventory-Sale Stockflow Inventory ItemID
Sale Number
Quantity
Actual Price
A-4 S-1 2 600 A-1 S-1 3 2,000 A-6 S-2 2 5,000 A-1 S-3 1 2,000 A-5 S-3 2 4,000 A-3 S-3 6 1,000 A-6 S-4 2 5,000 A-2 S-5 2 3,000 A-4 S-5 2 300 A-6 S-5 2 5,000 A-2 S-6 10 3,500 A-6 S-7 2 7,000 A-5 S-7 3 3,000
Sale Sale# Amount Date Cust# SalesRep# S-1 7,200 1 July C-1 E-12 S-2 10,000 21 July C-2 E-10 S-3 16,000 22 July C-5 E-10 S-4 10,000 26 July C-2 E-10 S-5 16,600 31 July C-5 E-10 S-6 35,000 15 Aug C-3 E-10 S-7 23,000 21 Aug C-4 E-99 Sale-CashRecDuality Sale# RA# Applied S-2 RA-1 1,666 S-4 RA-2 10,000 S-1 RA-3 7,200 S-3 RA-4 16,000 S-5 RA-4 16,600 S-2 RA-5 1,666 Customer Customer# Name A/R Amt SP# C-1 Bill E-12 C-2 Mick E-10 C-3 Keith E-10 C-4 Charlie E-99 C-5 Ron E-10
Cash Receipt Remittance Advice#
Amount
Bank Account#
Date
Customer Number
Cashier Number
RA-1 1,666 BA-6 25 July C-2 E-39 RA-2 10,000 BA-7 26 July C-2 E-39 RA-3 7,200 BA-7 15 Aug C-1 E-39 RA-4 32,600 BA-7 15 Aug C-5 E-39 RA-5 1,666 BA-6 25 Aug C-2 E-39
Cash Account# Type Bank Balance BA-6 Checking Boston5 BA-7 Checking Shawmut BA-8 Draft Shawmut 75,000 BA-9 Checking MassNat 0
Salesperson Employee Number
Quarterly Sales $
Comm rate
E-12 .12 E-10 .10 E-99 .10 E-78 0 .15
8
Relational Algebra SELECT
Find the cash receipts from Customer #2 (keeping all the details of those cash receipts)
Answer Remittance Advice#
Amount
Bank Account#
Date
Customer Number
Cashier Number
RA-1 1,666 BA-6 25 July C-2 E-39 RA-2 10,000 BA-7 26 July C-2 E-39 RA-5 1,666 BA-6 25 Aug C-2 E-39
Customer Customer# Name A/R Amt SP# C-1 Bill E-12 C-2 Mick E-10 C-3 Keith E-10 C-4 Charlie E-99 C-5 Ron E-10
Select Cash Receipt Where Customer Number = C-2 Giving Answer
Cash Receipt Remittance Advice#
Amount
Bank Account#
Date
Customer Number
Cashier Number
RA-1 1,666 BA-6 25 July C-2 E-39 RA-2 10,000 BA-7 26 July C-2 E-39 RA-3 7,200 BA-7 15 Aug C-1 E-39 RA-4 32,600 BA-7 15 Aug C-5 E-39 RA-5 1,666 BA-6 25 Aug C-2 E-39
9
Relational Algebra PROJECT
Find the customer number, name, and salesperson number for all customersCustomer Customer# Name A/R Amt SP# C-1 Bill E-12 C-2 Mick E-10 C-3 Keith E-10 C-4 Charlie E-99 C-5 Ron E-10
Salesperson Employee Number
Quarterly Sales $
Comm rate
E-12 .12 E-10 .10 E-99 .10 E-78 0 .15
Project Customer Over (Customer#, Name, SP#) Giving Answer
10
Join Types• Inner join
– includes only the records from both tables that have the exact same values in the fields that are joined
– I.e.,
• Outer join– includes all records from one table, and matche
s those records from the other table for which values in the joined fields are equal
– I.e.,
Left Outer Join Right Outer Join
11
Relational Algebra Inner Join
Find all details of all customers and all available details of each customer’s salesperson
Join Customer, Salesperson Where Customer.SP# = [Salesperson.Employee Number] Giving Answer
Customer Customer# Name A/R Amt SP# C-1 Bill E-12 C-2 Mick E-10 C-3 Keith E-10 C-4 Charlie E-99 C-5 Ron E-10
Salesperson Employee Number
Quarterly Sales $
Comm rate
E-12 .12 E-10 .10 E-99 .10 E-78 0 .15
12
Relational Algebra Left Outer Join
Answer Sale# Amount Date Cust# SalesRep# Sale# RA# Applied S-1 7,200 1 July C-1 E-12 S-1 RA-3 7,200 S-2 10,000 21 July C-2 E-10 S-2 RA-1 1,666 S-2 10,000 21 July C-2 E-10 S-2 RA-5 1,666 S-3 16,000 22 July C-5 E-10 S-3 RA-4 16,000 S-4 10,000 26 July C-2 E-10 S-4 RA-2 10,000 S-5 16,600 31 July C-5 E-10 S-5 RA-4 16,600 S-6 35,000 15 Aug C-3 E-10 S-7 23,000 21 Aug C-4 E-99
Find all details of all sales and the cash receipt number and amount applied of any cash receipts related to those sales
Left Outer Join Sale, [Sale - CashRecDuality] Where [Sale.Sale#] = [Sale - CashRecDuality.Sale#] Giving Answer
13
SQL (Structured Query Language)
• Each query statement follows the same structure:SELECT attribute name(s)FROM table name(s)WHERE criteria is met;
14
SQL Statements and Relational Algebra
• SQL’s SELECT component isolates columns– i.e., relational algebra’s project
• SQL’s FROM component is used for identifying the table(s) involved– if >1 table, helps accomplish relational algebra’
s join (together with WHERE component that specifies equal fields)
• SQL’s WHERE component isolates rows– i.e., relational algebra’s select– also helps accomplish relational algebra’s join– may be left blank for single-table queries that r
etrieve all rows
15
Find the cash receipts from Customer #2 (keeping all the details of those cash receipts)
SQL and Relational Algebra SELECT
Query Result Remittance Advice#
Amount
Bank Account#
Date
Customer Number
Cashier Number
RA-1 1,666 BA-6 25 July C-2 E-39 RA-2 10,000 BA-7 26 July C-2 E-39 RA-5 1,666 BA-6 25 Aug C-2 E-39
Customer Customer# Name A/R Amt SP# C-1 Bill E-12 C-2 Mick E-10 C-3 Keith E-10 C-4 Charlie E-99 C-5 Ron E-10
Cash Receipt Remittance Advice#
Amount
Bank Account#
Date
Customer Number
Cashier Number
RA-1 1,666 BA-6 25 July C-2 E-39 RA-2 10,000 BA-7 26 July C-2 E-39 RA-3 7,200 BA-7 15 Aug C-1 E-39 RA-4 32,600 BA-7 15 Aug C-5 E-39 RA-5 1,666 BA-6 25 Aug C-2 E-39
Select *From [Cash Receipt]Where [Customer Number] = C-2;
(note: the brackets are needed because of spaces in the table and field names; also note * is a wild card indicating all columns should be included)
16
SQL and Relational Algebra PROJECT
Query Result Customer# Name SP# C-1 Bill E-12 C-2 Mick E-10 C-3 Keith E-10 C-4 Charlie E-99 C-5 Ron E-10
Select Customer#, Name, SP#
From Customer;
Find the customer number, name, and salesperson number for all customersCustomer Customer# Name A/R Amt SP# C-1 Bill E-12 C-2 Mick E-10 C-3 Keith E-10 C-4 Charlie E-99 C-5 Ron E-10
Salesperson Employee Number
Quarterly Sales $
Comm rate
E-12 .12 E-10 .10 E-99 .10 E-78 0 .15
17
SQL and Relational Algebra Inner Join
Select *From Customer, SalespersonWhere Customer.SP# = [Salesperson.Employee Number];
Query Result Customer# Name A/R Amt SP# Employee
Number Quarterly
Sales$ Comm
rate C-1 Bill E-12 E-12 .12 C-2 Mick E-10 E-10 .10 C-3 Keith E-10 E-10 .10 C-4 Charlie E-99 E-99 .10 C-5 Ron E-10 E-10 .10
Find all details of all customers and all available details of each customer’s salesperson
18
SQL and Relational Algebra Outer Join
Query Result Sale# Amount Date Cust# SalesRep# Sale# RA# Applied S-1 7,200 1 July C-1 E-12 S-1 RA-3 7,200 S-2 10,000 21 July C-2 E-10 S-2 RA-1 1,666 S-2 10,000 21 July C-2 E-10 S-2 RA-5 1,666 S-3 16,000 22 July C-5 E-10 S-3 RA-4 16,000 S-4 10,000 26 July C-2 E-10 S-4 RA-2 10,000 S-5 16,600 31 July C-5 E-10 S-5 RA-4 16,600 S-6 35,000 15 Aug C-3 E-10 S-7 23,000 21 Aug C-4 E-99
Find all details of all sales and the cash receipt number and amount applied of any cash receipts related to those sales
Select *From Sale LeftJoin [Sale-CashRecDuality]Where [Sale.Sale#]=[Sale-CashRecDuality.Sale#];
19
Mathematical Comparison Operators
• SQL Queries may include mathematical comparison operators such as – = equal to– < less than– <= less than or equal to– > greater than– >= greater than or equal to– <> not equal to (or != in some software)
• Mathematical comparison operators are typically included in the WHERE clause of the SQL statement, and may be used on all types of fields– For date fields, dates that are earlier in time are “less than”
dates that are later in time.– For text fields, A < B < C, etc.
20
SQL Mathematical Comparison Operators
Select Account#, BalanceFrom CashWhere Balance>=50000;
Query Result Account# Type Bank Balance BA-8 Draft Shawmut 75,000
Cash Account# Type Bank Balance BA-6 Checking Boston5 BA-7 Checking Shawmut BA-8 Draft Shawmut 75,000 BA-9 Checking MassNat 0
21
SQL Mathematical Comparison Operators on Character Attributes
Select Sale#, AmountFrom SaleWhere SalesRep# <> E-10;
Query Result Sale# Amount S-1 7,200 S-7 23,000
22
Queries with Logical Operators
• Queries may include logical operators AND, OR, and NOT– AND accomplishes a set intersection – answer
includes all instances that meet BOTH conditions
– OR accomplishes a set union – answer includes all instances that meet one condition and all instances that meet the other condition
– NOT identifies instances that do not meet one or more conditions
23
Queries with Special Operators• BETWEEN is used to define the range limits.
– The end points of the range are included
Query Result Sale# Amount Date S-1 7,200 1 July S-2 10,000 21 July S-3 16,000 22 July S-4 10,000 26 July S-5 16,600 31 July
Select Sale#, Amount, DateFrom SaleWhere Date BETWEEN 7/1 and 7/31;
24
Queries with Special Operators
• IS NULL is used to retrieve attributes for which the value is null.
Query Result Account# Type Bank Balance BA-6 Checking Boston5 BA-7 Checking Shawmut
Select *From CashWhere Balance IS NULL;
Cash Account# Type Bank Balance BA-6 Checking Boston5 BA-7 Checking Shawmut BA-8 Draft Shawmut 75,000 BA-9 Checking MassNat 0
25
Queries with Special Operators• EXISTS is used to retrieve attributes for w
hich the value is not null.
Query Result Account# Type Bank Balance BA-8 Draft Shawmut 75,000 BA-9 Checking MassNat 0
Cash Account# Type Bank Balance BA-6 Checking Boston5 BA-7 Checking Shawmut BA-8 Draft Shawmut 75,000 BA-9 Checking MassNat 0
Select *From CashWhere Balance EXISTS;
26
Aggregation Functions in Queries• An aggregation function summarizes the data values
within a field (column)– COUNT summarizes the number of rows that conta
in 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 inclu
ded in the answer– MIN identifies the minimum (lowest) attribute value
for the field– MAX identifies the maximum (greatest) attribute val
ue for the field
27
Queries with Horizontal Calculations
• “Horizontal” calculations mathematically combine values from different fields for each row– Horizontal calculations should NOT be include
d in the same query as an aggregation function
• One query may perform a horizontal calculation and another query that builds on the first query may perform the aggregation function, or vice versa
– The “correct” order for the queries depends on the goal