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

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

Embed Size (px)

Citation preview

Page 1: 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

Page 2: Computer Science & Engineering 2111 Outer Joins 1CSE 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

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

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

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

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

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

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

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

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

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

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

CSE 2111 Lecture-Many to One to Many Relationships in Queries 7

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

Client

Charges

Payments

Resulting DynasetFirst-what should the results look like?

CSE 2111 Lecture-Many to One to Many Relationships in Queries 8

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

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

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

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

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

Charges

Client

Resulting Intermediate Dynaset 1 (Partial View)

11CSE 2111 Lecture-Many to One to Many Relationships in Queries

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

Intermediate Dynaset 1 Payments

Resulting Intermediate Dynaset 2 (Partial View)

12

$100.00

CSE 2111 Lecture-Many to One to Many Relationships in Queries

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

Aggregate functions & expressions are applied last:

Final Dynaset

Resulting Intermediate Dynaset 2

CSE 2111 Lecture-Many to One to Many Relationships in Queries 13

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Client Payments

Intermediate Dynaset 1

23CSE 2111 Lecture- One to Many to One Relationships in Queries

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

Intermediate Dynaset 1

PaymentMethod

Final Dynaset

24CSE 2111 Lecture- One to Many to One Relationships in Queries

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

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

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

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

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

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

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

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

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

Finally!

CSE 2111- NZ Function 29