Computer Science & Engineering 2111 Outer Joins 1CSE 2111 Lecture- Inner Vs. Outer Jioins
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 CSE 2111 Lecture- Inner Vs. Outer Jioins2
Outer Join between Client and Payments Outer join relative to the Client table (the primary key side of the relationship) Resulting Dynaset CSE 2111 Lecture- Inner Vs. Outer Jioins3
Outer Join between Client and Payments Outer join relative to the Payments table (the many side of the relationship) Resulting Dynaset CSE 2111 Lecture- Inner Vs. Outer Jioins4
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 Relationships Write 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 Dynaset First-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) 11 CSE 2111 Lecture-Many to One to Many Relationships in Queries
Intermediate Dynaset 1Payments Resulting Intermediate Dynaset 2 (Partial View) 12 $ 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: ClientIDJoin Type: Outer Field:ClientIDFirstNameLastNameAmount Table:Client Payments Total:Group By Sum Sort: Show:XXXX Criteria: Or: 15 CSE 2111 Lecture-Many to One to Many Relationships in Queries
ChargesByClient Tables: Client, Charges Join On: ClientID Join Type: Outer Field:ClientIDAmount Table:ClientCharges Total:Group BySum Sort: Show:XXXX Criteria: Or: 16 CSE 2111 Lecture-Many to One to Many Relationships in Queries
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 PaymentsByClientChargesByClient CSE 2111 Lecture-Many to One to Many Relationships in Queries 17
BalanceDue Tables: PaymentsByClient,ChargesByClient Join On: ClientIDJoin Type: Inner Field: ClientIDFirstNameLastNameSumOfAmount Balance* Table: PaymentsBy Client ChargesBy Client Total: Sort: Show: XXXXXX Balance: [ChargesByClient]![SumOfAmount] – [PaymentsByClient]![SumOfAmount] Now put the two summaries together & calculate the balance due… 18 CSE 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 21 CSE 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
ClientPayments Intermediate Dynaset 1 23 CSE 2111 Lecture- One to Many to One Relationships in Queries
Intermediate Dynaset 1 PaymentMethod Final Dynaset 24 CSE 2111 Lecture- One to Many to One Relationships in Queries
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. CSE NZ Function25
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 NZ Function26
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 NZ Function27
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….. CSE NZ Function28
Finally! CSE NZ Function29