You are on page 1of 29

Report Studio

Understanding Joins

Cardinality

Report Studio Joins, Intersects, Excepts, Unions

Documentation Developed by Elisabetta Zodeiko, Princeton University


March 2011
Princeton University

From Cognos: in general, join relationships should be created in the Framework Manager model.
Create a join relationship in Report Studio if what you are trying to do cannot be modeled in
Framework Manager.

What is a “Query”?

SQL (Structured Query Language) is a standard language for accessing databases. An example of a
SQL statement used in an Oracle database is:

Select * from Personal_Data_Table where Last_Name = ‘Smith’;

Select * from Personal_Data_Table where Last Name = ‘Smith’;

Select all records from the Personal Data Table which have “Smith” as the
Last Name field. The
semicolon ends the
statement.

What is a “Join”?

A Join is a relationship between two database tables, created to combine records from each table.
A Join is a combination of two database tables.

A Join Relationship Example:

The Person Table (containing personal columns) is joined to the Citizenship table (containing
citizenship columns) so that all the records, pertaining to both personal and citizenship, can be
joined together and displayed in one query, or in one report.
Personal Data Table 1 Citizenship Data Table 2
---ID Field (00000012) ---ID Field (00000012)
---First Name Field (Joe) ---Citizenship Status Field (A1)
---Last Name Field (Smith) ---Citizenship Status Descr Field
(Active)
---City Field (Princeton) ---Country of Citizenship Field (USA)
---State Field (NJ) ---Country of Citizenship
Description Field (United States
of America)
---Country Descr Field
(USA)
---Gender Field (M)

Report Studio Joins pg. 2


Princeton University

After joining the two tables:


Joined Table 3 (Personal Data & Citizenship Data)
---ID Column
---First Name Column
---Last Name Column
---City Column
---State Column
---Country Descr Column
---Gender Column
---ID Column
---Citizenship Status Column
---Citizenship Status Descr Column
---Country of Citizenship Column
--- Country of Citizenship Description Field Column

The Query behind the Joined Table 3 can be displayed as a report.

The Query for Joined Table 3 would be:

Select * from Personal_Data_Table join Citizenship Data on ID = ID;

Select all records from the Personal Data Table matching records on the ID columns.

Report Studio Joins pg. 3


Princeton University

Join Types: Inner, Outer, Left, Right, Cartesian Product.


An INNER Join returns rows when there is at least one matching record found in both tables.

Visually, an INNER Join can be seen in through Venn diagram

Join Results
Table A Table B

Table A Table B
EmplID Country EmplID Dept
123 USA 120 HR
124 USA 121 HR
125 USA 123 HR
130 Mexico 124 HR
131 Mexico 125 HR
132 Mexico 130 Finance
140 Mexico 133 Finance
Total: 7 Records 140 OIT
141 OIT
142 OIT
Total: 10 Records

Select A.Emplid, A.Dept from Table A INNER JOIN Table B on A.Emplid = B.Emplid;

This is how the Join happens

A total of 5 records will be returned because the Query only returns records which have an EmplID
in both Table A and Table B: EmplIDs 123, 124, 125, 130, 140.

Report Studio Joins pg. 4


Princeton University

An OUTER Join returns rows when there are records from Table A even if there are no matching
records in Table B (LEFT OUTER Join), or vice-versa, the OUTER Join can return rows when there are
records from Table B even if there are no matching records in Table A (RIGHT OUTER Join),.

Visually, an OUTER Join (both LEFT and RIGHT) can be seen in through Venn diagrams

Table A Table B Table A Table B


Join Results Join Results

LEFT OUTER Join RIGHT OUTER Join

Table A Table B
EmplID Country EmplID Dept
123 USA 120 HR
124 USA 121 HR
125 USA 123 HR
130 Mexico 124 HR
131 Mexico 125 HR
132 Mexico 130 Finance
140 Mexico 133 Finance
Total: 7 Records 140 OIT
141 OIT
142 OIT
Total: 10 Records

Select A.Emplid, A.Dept from Table A OUTER JOIN Table B on A.Emplid = B.Emplid;

This is how the LEFT OUTER Join happens

Report Studio Joins pg. 5


Princeton University

A total of 7 records will be returned because the Query only returns records which have an EmplID
and Dept from Table A which matches exactly in Table B. EmplIDs 123, 124, 125, 130, 131, 132, 140.

Query Results
EmplID Dept
123 HR
124 HR
125 HR
130 B
131 NULL (no record
of 131 in Table B)
132 NULL (no record
of 132 in Table B)
140 C
Total: 7 Records

A LEFT OUTER JOIN returns all the rows from the left table, or the first table, mentioned in the
Query, even if there are no matches in the right table (second table).

A RIGHT OUTER JOIN returns all the rows from the right table, or the second table, mentioned in the
Query, even if there are no matches in the left table (first table).

Report Studio Joins pg. 6


Princeton University

Cartesian Production
A Cartesian product, also known as a “Cross Join” is a relationship which joins each row from Table A
to each row in Table B. This happens because no relationship is defined in the Query.

Select A.EmplID, B.Dept

Notice there is no “where” clause to define the relationship.

If a Cartesian product, or cross join, was created between Table A and Table B below, the resulting
Table would have 70 rows.

Table A Table B
EmplID Country EmplID Dept
123 A 120 A
124 A 121 A
125 A 123 A
130 B 124 A
131 B 125 A
132 B 130 B
140 B 133 B
Total: 7 Records 140 C
141 C
142 C
Total: 10 Records

Query Results
EmplID Dept
123 A
123 A
123 A
123 A
123 A
123 B
123 B
123 C
123 C
123 C
124 A
124 A
124 A
124 A

Report Studio Joins pg. 7


Princeton University

124 A
124 B
124 B
124 C
124 C
124 C
etc…
Total: 70 Records

Report Studio Joins pg. 8


Princeton University

What is Cardinality?
The term cardinality refers to the uniqueness of data values contained in a particular column
(attribute) of a database table. The lower the cardinality, the more duplicated elements in a column.

In Report Studio, Cardinality is a visual representation of the type of Join, either Inner, Outer, Left, or
Right.

Cardinality can be expressed as:


 One-to-one relationship (1:1)
 One-to-many relationship (1:N)
 Zero-to-one (0:1)
 Zero-to-many (0:N)

A one-to-one Cardinality would look like:

Each “ID” record in the Personal Table has one and only one matching “ID” record in the Citizen
Table.

Each “ID” record in the Citizen Table has one and only one matching “ID” records in the Personal
Table.

(This is an Inner Join)

Report Studio Joins pg. 9


Princeton University

In this join, the Cardinality is a one-to-many relationship, meaning that:

Each “ID” record in the Personal Table has one or more than one matching “ID” record, in the Citizen
Table.

Each “ID” record in the Citizen Table has one or more than one matching “ID” record in the Personal
Table.

(This is an Inner Join)

Report Studio Joins pg. 10


Princeton University

A zero-to-one Cardinality would look like:

Each “ID” record in the Personal Table has zero or only one matching “ID” record, in the Citizen
Table.

Each “ID” record in the Citizen Table has zero or only one matching “ID” record in the Personal
Table.

(This is an Outer Join)

Report Studio Joins pg. 11


Princeton University

A zero-to-many Cardinality would look like:

Each “ID” record in the Personal Table has zero or many matching “ID” record, in the Citizen Table.

Each “ID” record in the Citizen Table has zero or many matching “ID” record in the Personal Table.

(This is an Outer Join)

Report Studio Joins pg. 12


Princeton University

Referring to the definition of a LEFT JOIN:

“A LEFT JOIN returns all the rows from the left table, or the first table, mentioned in the Query, even
if there are no matches in the right table (second table)”

A LEFT OUTER JOIN, using a zero-to-many relationship, would look like:

Each “ID” record in the Personal Table has zero or many matching “ID” record, in the Citizen Table.

Each “ID” record in the Citizen Table has one or many matching “ID” record in the Personal Table.

Report Studio Joins pg. 13


Princeton University

Referring to the definition of a RIGHTJOIN:

“A RIGHT JOIN returns all the rows from the right table, or the second table, mentioned in the
Query, even if there are no matches in the left table (first table).

A RIGHT OUTER JOIN, using a zero-to-many relationship, would look like:

Each “ID” record in the Personal Table has one or many matching “ID” record, in the Citizen Table.

Each “ID” record in the Citizen Table has none or many matching “ID” record in the Personal Table.

Report Studio Joins pg. 14


Princeton University

Report Studio Terms


Report Studio lends the ability to create joins between Query Subjects. Usually these joins between
Query Subjects are completed using the Cognos Framework Manager tool, and are already available
to the report writer in Report Studio. However, the report writer can choose to create his own joins,
either between Query Subjects which have not been joined using the Framework Manager tool, or
those that have been joined, for which the report writer needs to enforce his own join relationship,
overriding the relationship defined in Framework Manager.

From Cognos: in general, join relationships should be created in the Framework Manager model.
Create a join relationship in Report Studio if what you are trying to do cannot be modeled in
Framework Manager.

In the Cognos 8 Training Package, none of the three available Query Subjects, Personal Data,
Citizenship Data, and Job Data, are joined to any other Query Subject. The only way to retrieve data
from more than one Query Subject into one report is to create a join between the Query Subjects.

The following four Exercises will explore the different types of Joins we can create in Report Studio.

Exercise One - Join

The Personal Data Query Subject contains data about someone’s address. The Job Query Subject
contacts data about someone’s job information. Joining the two will allow a report to be written
showing both sets of data.

We will create two queries, each one holding either a set of Personal Data columns or Job Data
columns. We will then create a join relationship, specifying a one-to-many relationship on the
EmplIDs from each field. Finally, we will then create a third query to contain both sets of columns.

Query Subjects:
1. Personal Data containing 1059 records
2. Job Data containing 34 records
3. Result: 32 Records (with Auto Aggregation). 34 Records (without Auto Aggregation).

Report Studio Joins pg. 15


Princeton University

Report Studio Joins pg. 16


Princeton University

Steps:

Create two queries.

1. Open Report Studio. Select a “List” type report. Save this report as “Join Personal & Job Data”.
2. Mouse over the Query Explorer and click on yellow “Queries” icon.
3. Click once on the blue “Query 1” icon.
4. In the Properties area, in the “Name” option, rename “Query1” to “PersonalQuery”.

Report Studio Joins pg. 17


Princeton University

5. Double click on the PersonalQuery icon. Once inside the query, add the following columns from
the “Personal Data” Query Subject, found on the Source tab.

 ID
 Last Name
 First Name
 Street Address 1
 City
 State
 Postal Code

6. Mouse over the Query Explorer and click on yellow “Queries” icon.
7. From the Toolbox tab, drag over a Query icon, placing below the PersonalQuery.

Report Studio Joins pg. 18


Princeton University

8. In the Properties area, in the “Name” option, rename “Query1” to “JobQuery”.

9. Double click on the JobQuery icon. Once inside the query, add the following columns from the
“Job Data” Query Subject, found on the Source tab.

We are ready to join both Queries.

Report Studio Joins pg. 19


Princeton University

10. Mouse over the Query Explorer and click on yellow “Queries” icon.

11. From the Toolbox tab, drag over a yellow “Join” icon and place it below JobQuery.
12. Drag the PersonalQuery into the first block of the Join (called a “Join Operand”).
13. Drag the JobQuery into the second block of the Join.

14. Double click on the Join icon. A new window will open. Here we will create the join
relationship, setting the Cardinality to a one-to-many relationship.

Report Studio Joins pg. 20


Princeton University

15. Click the “New Link” button. A new link will be created between the first column listed under
each Query. (To change which columns are linked, click on the desired column name).

16. Set the Cardinality to a one-to-many relationship.

Each “ID” record in the Personal Table has one or many matching “ID” records, in the Citizen Table.

Each “ID” record in the Citizen Table has one or many matching “ID” records in the Personal Table.

17. Click “OK” to exit the Join Relationships window.

18. Click once on Query One found to the left of the Join icon. This is the query which is the
recipient of the joins.

19. In the Properties area, in the “Name” option, rename “Query1” to “JoinPersonJobQuery”.

Report Studio Joins pg. 21


Princeton University

20. Double-click on JoinPersonJobQuery. From the Source Tab, add the following columns from
both the PersonalQuery and JobQuery, into the “Data Items” section of the page:

PersonalQuery JobQuery
ID ID
Last Name Full Name
First Name Last Name
Street Address 1 First Name
City Middle Name
State Dept ID
Postal Code Dept Name
Salary

These are the fields which will be used in the PersonalJobQuery to populate the list report, on the
Report Page.

21. Mouse over the “Page Explorer” and click on “Page1”.

Report Studio Joins pg. 22


Princeton University

22. Click anywhere on the List. In the Properties area, click up the Ancestor arrow button, and
select “List”. The entire List will be highlighted grey.

23. In the Properties section, in the Query area, change the Query which supplies the List to
“PersonalJobQuery”.

Report Studio Joins pg. 23


Princeton University

24. From the “Data Items” tab, located to the right of the Source tab, add the following columns to
the List Report from the JoinPersonJobQuery.

 ID
 Last Name
 First Name
 Street Address 1
 City
 State
 Postal Code
 Dept ID
 Dept Name
 Salary

25. To set the number of rows which appear in the report on one page, click anywhere in the list. In
the Properties area, click up the Ancestor arrow button, and select “List”. The entire List will be
highlighted grey. In the Rows Per Page property, set the number of rows to “1500”.

26. To sort records in the ID Column, click on the ID Colum, in the blue List Column Title area.

27. With the ID List Column Title area highlighted, click the “Sort” button. The ID Column will now
be sorted.

28. To count the number of IDs appearing in the list, click on the ID column, in the blue List Column
Title area.

29. With the ID List Column Title area highlighted, click the Aggregate Button in the toolbar, and
select “Count”. A new summary row should appear in the list.

Report Studio Joins pg. 24


Princeton University

30. Run the report

Result: Notice that there are 32 rows returned, yet there are 34 rows in the Job Table, and 1059 in
the Personal Data table. The reason for this is that by default, a Join in Report Studio is an
INNER JOIN, and our Cardinality specified there should be one record…….

Also, Auto Aggregation….

Finish

Report Studio Joins pg. 25


Princeton University

Turn Auto Aggregation Off

Using the results from the Job Personal Report, in the previous “Join” exercise, we notice there are
32 records returned. There is only one record returned for ID #000000012, Yat-Tze Huang,
stating a Salary entry for “$457,500”. However, this person actually has two records in the Job
Table, each record containing a different Salary entry. The “Salary” column in the Job Table is a
numeric field. All numeric records are “Auto Aggregated” in Report Studio, meaning that
multiple records for one unique ID are combined into one row. To see each record broken out,
so that in this example there would be two rows returned for Yat-Tze Huang, each one showing
the different Salary entries, the “Auto Aggregation” feature in Report Studio needs to be turned
off.

1. Open the previous “Join Personal & Job Data” report created in the Join Exercise above. Rename
it “Auto Aggregation Off” and save the report.

In a report which displays a list, based on the results of a Join, the auto-aggregation property is
turned off on the column in the source query, not in the resulting joined query.

2. Mouse over the Query Explorer and click on “JobQuery”.

3. Click once on the “Salary” data item.

4. In the Properties area, change the Aggregate Function Property to “None”.

Report Studio Joins pg. 26


Princeton University

5. Run the report. A total of 34 records should be returned, with two Salary rows returned for ID
“000000012”.

Finish.

Report Studio Joins pg. 27


Princeton University

Exercise Two - Union

A Union Join combines the records of two tables together. It is similar to a Join, but the restriction
of a Union Join is that all corresponding columns need to be of the same data type and only distinct
values are selected. Said another way, it removes duplicate rows between the various “select”
statements.

The Personal Data Query Subject contains data about someone’s address. The Citizenship Query
Subject contacts data about someone’s citizenship information. Joining the two will allow a report
to be written showing both sets of data.

We will create two queries, each one holding either a set of Personal Data columns or Citizenship
Data columns. We will then create a join relationship, specifying a one-to-many relationship on the
EmplIDs from each field. Finally, we will then create a third query to contain both sets of columns.

Query Subjects:
1. Personal Data containing 1059 records
2. Citizenship Data containing 1049 records

Exercise Three - Intersect

A Intersect Join combines the records of two queries together and returns only rows that appear in
both queries.

SELECT * FROM Table A WHERE EmplID BETWEEN 1 AND 100

INTERSECT

SELECT * FROM Table A WHERE EmplID BETWEEN 50 AND 200;

Report Studio Joins pg. 28


Princeton University

Exercise Four - Except

An Except Join finds the distinct rows from one query and returns only those rows that do not
appear in query two.

SELECT *
FROM Table A
WHERE EmpID BETWEEN 00000001 AND 00000100

EXCEPT

SELECT *
FROM Table A
WHERE EmpID BETWEEN 00000050 AND 00000075;

Report Studio Joins pg. 29

You might also like