Download presentation
Presentation is loading. Please wait.
Published byJasper Welch Modified over 9 years ago
2
Computer Science & Engineering 2111 Lecture 13 Outer Joins 1
3
Research Papers Database Primary Keys Foreign Keys 2
4
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 Dynaset PK FK 3
5
Outer Join between Client and Payments Outer join relative to the Client table (the primary key side of the relationship) Resulting Dynaset 4
6
Outer Join between Client and Payments Outer join relative to the Payments table (the many side of the relationship) Notice the results look very similar to an inner join between the two tables with one major exception. Can you identify the exception? Resulting Dynaset 5
7
Using 3 or more tables in a query If I needed to obtain records from my database that included the customer information (name and ID), total charges, total payments and balance due, I would need to run a query using these tables. What would happen? Let’s look at a Many-One-Many Relationship and see what happens 6
8
Running a Query with Client, Charges and Payments: The Design View 7
9
Client Charges Payments Resulting Dynaset First-what should the results look like? 8
10
What we actually get Should Nancy have total charges of $750 and total payments of $700? Where are the other clients? Karen Day has charges but no payments. Shouldn’t you want to see her in your results? Are there more people like this missing? Now let’s see what really happens….. What we WANT 9
11
So what happened? Clients Charges Payments Intermediate Dynaset 1 Intermediate Dynaset 2 Final Dynaset Aggregate functions applied/Expressions calculated 10
12
Charges Client Resulting Intermediate Dynaset 1 (Partial View) 11
13
Intermediate Dynaset 1 (Partial View)Payments Resulting Intermediate Dynaset 2 12
14
Aggregate functions & expressions are applied last: Final Dynaset Resulting Intermediate Dynaset 2 13
15
2 Major Problems exist with our results Not all of the Clients show up in the final dynaset. – We used an Inner Join and got only those clients who are in both the Charges table and the Payments table. – Any ClientID that is not in all 3 tables will be left out of our final results. For some of our clients, their charges and payments are wrong, resulting in an incorrect balance! 14
16
Would it help to use an Outer Join? Actually it did- but not enough. It solved the problem of not including everyone unless they were in all 3 tables. So now we see all clients, but some of them still have wrong values for charges and payments. Notice some of the values are correct making this a very dangerous problem. If you only spot check a few values, you might not see the problem. 15
17
Good news and Bad News Good News: Access doesn’t always mess up queries with 3 or more tables in it. – We can predict and avoid this problem! Bad News: You have to know what to look for to prevent this kind of problem from happening. 16
18
Using 3 or more tables in a query If I needed to obtain records from my database that included the customer information (name and ID), and the Method Type, I would need to run a query using these tables. What would happen? Let’s look at a One-Many-One Relationship and see what happens 17
19
What we get Now let’s see what really happens….. What we WANT 18
20
ClientPayments Intermediate Dynaset 1 19
21
Intermediate Dynaset 1 PaymentMethod Final Dynaset 20
22
So what’s the difference? Many-One-Many NOT VALID!! One-Many-One OK! 21
23
1 ∞ Client Charges 1 Payments Client ∞ Split up the relationship! 22 So what do we do? SUMMARIZE CHARGES BY CLIENT SUMMARIZE PAYMENTS BY CLIENT
24
PaymentsByClient Tables: Client, Payments Join On: ClientIDJoin Type: Outer Field:ClientIDFirstNameLastNameAmount Table:Client Payments Total:Group By Sum Sort: Show:XXXX Criteria: Or: 23
25
ChargesByClient Tables: Client, Charges Join On: ClientID Join Type: Outer Field:ClientIDAmount Table:ClientCharges Total:Group BySum Sort: Show:XXXX Criteria: Or: 24
26
Notice that each client is listed exactly once in both queries. 1 ChargesByClient PaymentsByClient 1 Now we can put the relationship back together! Join on ClientID PaymentsByClient ChargesByClient 25
27
BalanceDue Tables: PaymentsByClient,ChargesByClient Join On: ClientIDJoin Type: Inner Field: ClientIDFirstNameLastNameSumOfAmount Balance* Table: PaymentsBy Client ChargesBy Client Total: Sort: Show: XXXXXX Balance: [Charges]![SumOfAmount] – [Payments]![SumOfAmount] Now put the two summaries together & calculate the balance due… 26
28
Are we there yet? Not quite….notice that Karen Day was charged $100 but hasn’t made a payment. Her balance should be $100 - $0 = $100, but it’s blank. Why? 27
29
ClientPayments 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. 28
30
Access doesn’t know what $100 – NULL is, so it punts and returns NULL as the result. But we know that in this case, NULL should be treated like zero – can we help Access out? 29
31
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. 30
32
BalanceDue Tables: PaymentsByClient,ChargesByClient Join On: ClientIDJoin Type: Inner Field: ClientIDFirstNameLastNameSumOfAmount Balance* Table: PaymentsBy Client ChargesBy Client Total: Sort: Show: XXXXXX Balance: Nz([Charges]![SumOfAmount],0) – Nz([Payments]![SumOfAmount],0) Balance Due with the Nz function….. 31
33
Finally! 32
34
Summary Inner joins include only those combined records where the primary & foreign keys match. Outer joins include all records from one of the tables, even if there isn’t a matching record in the other table. Many-one-many relationships are not valid and must be broken down into multiple valid (one- many) relationships. Use Nz to replace NULL values with zeroes. 33
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.