Computer Science & Engineering 2111 Outer Joins 1CSE 2111 Lecture- Inner Vs. Outer Jioins.
-
Upload
victor-underwood -
Category
Documents
-
view
227 -
download
0
Transcript of Computer Science & Engineering 2111 Outer Joins 1CSE 2111 Lecture- Inner Vs. Outer Jioins.
1
Computer Science & Engineering 2111
Computer Science & Engineering 2111
Outer Joins
CSE 2111 Lecture- Inner Vs. Outer Jioins
CSE 2111 Lecture- Inner Vs. Outer Jioins
2
Inner Join between Client and Payments
Notice that only records with matching values in the foreign key fields of the related tables are included in the resulting dynaset
Resulting DynasetPK FK
CSE 2111 Lecture- Inner Vs. Outer Jioins
3
Outer Join between Client and PaymentsOuter join relative to the Client table (the primary key side of the relationship)
Resulting Dynaset
CSE 2111 Lecture- Inner Vs. Outer Jioins
4
Outer Join between Client and PaymentsOuter join relative to the Payments table (the many side of the relationship)
Resulting Dynaset
Valid and Invalid Relationships in Queries
Many-One-Many NOT VALID!!
One-Many-One OK!
CSE 2111 Lecture-Many to One to Many Relationships in Queries 5
Many to One to Many RelationshipsWrite a query to summarize by client, their total charges, total payments and balance due. List the Client ID, First Name, Last Name, total charges, total payments, and balance.
CSE 2111 Lecture-Many to One to Many Relationships in Queries 6
Running a Query with Client, Charges and Payments: The Design View
CSE 2111 Lecture-Many to One to Many Relationships in Queries 7
Client
Charges
Payments
Resulting DynasetFirst-what should the results look like?
CSE 2111 Lecture-Many to One to Many Relationships in Queries 8
What we actually get
• Should Nancy have total charges of $750 and total payments of $700?
Now let’s see what really happens…..What we WANT
9
What we GET
CSE 2111 Lecture-Many to One to Many Relationships in Queries
So what happened?
Clients
Charges
Payments
Intermediate Dynaset 1
Intermediate Dynaset 2
Final Dynaset
Aggregate functions applied/Expressions
calculated
CSE 2111 Lecture-Many to One to Many Relationships in Queries 10
Charges
Client
Resulting Intermediate Dynaset 1 (Partial View)
11CSE 2111 Lecture-Many to One to Many Relationships in Queries
Intermediate Dynaset 1 Payments
Resulting Intermediate Dynaset 2 (Partial View)
12
$100.00
CSE 2111 Lecture-Many to One to Many Relationships in Queries
Aggregate functions & expressions are applied last:
Final Dynaset
Resulting Intermediate Dynaset 2
CSE 2111 Lecture-Many to One to Many Relationships in Queries 13
1
∞
Client
Charges
1
Payments
Client
∞
Split up the relationship!
14
So what do we do?
SUMMARIZE CHARGES BY CLIENT
SUMMARIZE PAYMENTS BY CLIENT
CSE 2111 Lecture-Many to One to Many Relationships in Queries
PaymentsByClient Tables: Client, Payments
Join On: ClientID Join Type: Outer
Field: ClientID FirstName LastName Amount
Table: Client Client Client Payments
Total: Group By Group By Group By Sum
Sort:
Show: X X X X
Criteria:
Or:
15CSE 2111 Lecture-Many to One to Many Relationships in Queries
ChargesByClient Tables: Client, Charges
Join On: ClientID Join Type: OuterField: ClientID Amount
Table: Client Charges
Total: Group By Sum
Sort:
Show: X X X X
Criteria:
Or:
16CSE 2111 Lecture-Many to One to Many Relationships in Queries
Notice that each client is listed exactly once in both queries.
1ChargesByClientPaymentsByClient
1
Now we can put the relationship back together!
Join on ClientID
PaymentsByClient ChargesByClient
CSE 2111 Lecture-Many to One to Many Relationships in Queries 17
BalanceDue Tables: PaymentsByClient,ChargesByClientJoin On: ClientID Join Type: Inner
Field: ClientID FirstName LastName SumOfAmount SumOfAmount Balance*
Table: PaymentsByClient
PaymentsByClient
PaymentsByClient
PaymentsByClient
ChargesByClient
Total:
Sort:
Show: X X X X X X
Balance: [ChargesByClient]![SumOfAmount] – [PaymentsByClient]![SumOfAmount]
Now put the two summaries together & calculate the balance due…
18CSE 2111 Lecture-Many to One to Many Relationships in Queries
Final Dynaset
Karen Day was charged $100 but hasn’t made a payment. Her balance should be $100 - $0 = $100, but it’s blank. Why?CSE 2111 Lecture-Many to One to Many
Relationships in Queries 19
Many-One-Many NOT VALID!!
One-Many-One OK!
20
Using 3 or more tables in a query
CSE 2111 Lecture- One to Many to One Relationships in Queries
Using 3 or more tables in a query
Write a query to list the Client ID, first name, last name, and the Method Type, for all Clients.
One-Many-One Relationship
21CSE 2111 Lecture- One to Many to One Relationships in Queries
What we get
Now let’s see what really happens…..
What we WANT
CSE 2111 Lecture- One to Many to One Relationships in Queries 22
Client Payments
Intermediate Dynaset 1
23CSE 2111 Lecture- One to Many to One Relationships in Queries
Intermediate Dynaset 1
PaymentMethod
Final Dynaset
24CSE 2111 Lecture- One to Many to One Relationships in Queries
Client Payments
Let’s take a closer look at the PaymentsByClient Query….in an outer join with respect to Clients, when a record from Clients doesn’t have any matching records in Payments, it’s included in the results, but the fields that would have come from Payments are NULL.
CSE 2111- NZ Function 25
Access doesn’t know what $100 – NULL is, so it returns NULL as the result.
But we know that in this case, NULL should be treated like zero – can we help Access out?
CSE 2111- NZ Function 26
NZ Function
Syntax: Nz(variant, value_if_null)
If this argument evaluates to NULL….
Return this value
If the variant argument does NOT evaluate to NULL, Nz will return whatever the variant argument does evaluate to.
CSE 2111- NZ Function 27
BalanceDue Tables: PaymentsByClient,ChargesByClientJoin On: ClientID Join Type: Inner
Field: ClientID FirstName LastName SumOfAmount SumOfAmount Balance*
Table: PaymentsByClient
PaymentsByClient
PaymentsByClient
PaymentsByClient
ChargesByClient
Total:
Sort:
Show: X X X X X X
Balance: Nz([Charges]![SumOfAmount],0) – Nz([Payments]![SumOfAmount],0)
Balance Due with the Nz function…..
CSE 2111- NZ Function 28
Finally!
CSE 2111- NZ Function 29