33
Computer Science & Engineering 2111 Lecture 13 Outer Joins 1

Computer Science & Engineering 2111 Lecture 13 Outer Joins 1

Embed Size (px)

Citation preview

Advance Queries in Access

Computer Science & Engineering 2111

Lecture 13Outer Joins

1This lecture will cover the use of some basic functions provided by EXCEL. We will be explore how these functions work and how they can be used to solve problems.

Research Papers DatabasePrimary KeysForeign Keys2Inner Join between Client and PaymentsNotice that only records with matching values in the foreign key fields of the related tables are included in the resulting dynaset

Resulting DynasetPKFK3Outer Join between Client and PaymentsOuter join relative to the Client table (the primary key side of the relationship)

Resulting Dynaset4Outer Join between Client and PaymentsOuter 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 Dynaset5Using 3 or more tables in a queryIf 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?

Lets look at a Many-One-Many Relationship and see what happens

6Running a Query with Client, Charges and Payments: The Design View7

Client

Charges

PaymentsResulting DynasetFirst-what should the results look like?

8What we actually getShould Nancy have total charges of $750 and total payments of $700?

Where are the other clients? Karen Day has charges but no payments. Shouldnt you want to see her in your results? Are there more people like this missing?Now lets see what really happens..

What we WANT

9So what happened?ClientsChargesPaymentsIntermediate Dynaset 1Intermediate Dynaset 2Final DynasetAggregate functions applied/Expressions calculated

10

ChargesClient

Resulting Intermediate Dynaset 1(Partial View)

11

Intermediate Dynaset 1 (Partial View)

PaymentsResulting Intermediate Dynaset 2

?

12Aggregate functions & expressions are applied last:Final DynasetResulting Intermediate Dynaset 2

132 Major Problems exist with our resultsNot 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!14Would 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.

15Good news and Bad NewsGood News: Access doesnt 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.

16Using 3 or more tables in a queryIf 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?

Lets look at a One-Many-One Relationship and see what happens17

What we getNow lets see what really happens..What we WANT18

Client

Payments

Intermediate Dynaset 119

Intermediate Dynaset 1

PaymentMethod

Final Dynaset20

So whats the difference?

Many-One-Many NOT VALID!!One-Many-One OK!211ClientCharges1PaymentsClientSplit up the relationship!22So what do we do?SUMMARIZE CHARGES BY CLIENTSUMMARIZE PAYMENTS BY CLIENTPaymentsByClientTables: Client, PaymentsJoin On: ClientIDJoin Type: OuterField:ClientIDFirstNameLastNameAmountTable:ClientClientClientPaymentsTotal:Group ByGroup ByGroup BySumSort:Show:XXXXCriteria:Or:

23ChargesByClientTables: Client, ChargesJoin On: ClientIDJoin Type: OuterField:ClientIDAmountTable:ClientChargesTotal:Group BySumSort:Show:XXXXCriteria:Or:

24Notice that each client is listed exactly once in both queries.1ChargesByClientPaymentsByClient1Now we can put the relationship back together!Join on ClientID

PaymentsByClientChargesByClient25BalanceDueTables: PaymentsByClient,ChargesByClientJoin On: ClientIDJoin Type: InnerField:ClientIDFirstNameLastNameSumOfAmountSumOfAmountBalance*Table:PaymentsByClientPaymentsByClientPaymentsByClientPaymentsByClientChargesByClientTotal:Sort:Show:XXXXXXBalance: [Charges]![SumOfAmount] [Payments]![SumOfAmount]Now put the two summaries together & calculate the balance due26

Are we there yet? Not quite.notice that Karen Day was charged $100 but hasnt made a payment. Her balance should be $100 - $0 = $100, but its blank. Why?27

Client

Payments

Lets take a closer look at the PaymentsByClient Query.in an outer join with respect to Clients, when a record from Clients doesnt have any matching records in Payments, its included in the results, but the fields that would have come from Payments are NULL. ?28Access doesnt 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?

29NZ FunctionSyntax: Nz(variant, value_if_null)If this argument evaluates to NULL.Return this valueIf the variant argument does NOT evaluate to NULL, Nz will return whatever the variant argument does evaluate to. 30BalanceDueTables: PaymentsByClient,ChargesByClientJoin On: ClientIDJoin Type: InnerField:ClientIDFirstNameLastNameSumOfAmountSumOfAmountBalance*Table:PaymentsByClientPaymentsByClientPaymentsByClientPaymentsByClientChargesByClientTotal:Sort:Show:XXXXXXBalance: Nz([Charges]![SumOfAmount],0) Nz([Payments]![SumOfAmount],0)Balance Due with the Nz function..31Finally!

32SummaryInner 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 isnt 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