Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science & Engineering 2111 Outer Joins 1CSE 2111 Lecture- Inner Vs. Outer Jioins.

Similar presentations


Presentation on theme: "Computer Science & Engineering 2111 Outer Joins 1CSE 2111 Lecture- Inner Vs. Outer Jioins."— Presentation transcript:

1 Computer Science & Engineering 2111 Outer Joins 1CSE 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 Dynaset PK FK CSE 2111 Lecture- Inner Vs. Outer Jioins2

3 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

4 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

5 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

6 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

7 Running a Query with Client, Charges and Payments: The Design View CSE 2111 Lecture-Many to One to Many Relationships in Queries 7

8 Client Charges Payments Resulting Dynaset First-what should the results look like? CSE 2111 Lecture-Many to One to Many Relationships in Queries 8

9 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

10 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

11 Charges Client Resulting Intermediate Dynaset 1 (Partial View) 11 CSE 2111 Lecture-Many to One to Many Relationships in Queries

12 Intermediate Dynaset 1Payments Resulting Intermediate Dynaset 2 (Partial View) 12 $ 100.00 CSE 2111 Lecture-Many to One to Many Relationships in Queries

13 Aggregate functions & expressions are applied last: Final Dynaset Resulting Intermediate Dynaset 2 CSE 2111 Lecture-Many to One to Many Relationships in Queries 13

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 ClientPayments Intermediate Dynaset 1 23 CSE 2111 Lecture- One to Many to One Relationships in Queries

24 Intermediate Dynaset 1 PaymentMethod Final Dynaset 24 CSE 2111 Lecture- One to Many to One Relationships in Queries

25 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 2111- NZ Function25

26 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 Function26

27 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 Function27

28 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 2111- NZ Function28

29 Finally! CSE 2111- NZ Function29


Download ppt "Computer Science & Engineering 2111 Outer Joins 1CSE 2111 Lecture- Inner Vs. Outer Jioins."

Similar presentations


Ads by Google