Professional Documents
Culture Documents
Understanding Joins
Cardinality
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 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.
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)
Select all records from the Personal Data Table matching records on the ID columns.
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;
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.
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
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;
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).
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.
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
124 A
124 B
124 B
124 C
124 C
124 C
etc…
Total: 70 Records
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.
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.
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.
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.
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.
“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)”
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.
“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).
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.
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.
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).
Steps:
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”.
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.
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.
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.
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).
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.
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”.
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.
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”.
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.
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…….
Finish
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.
5. Run the report. A total of 34 records should be returned, with two Salary rows returned for ID
“000000012”.
Finish.
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
A Intersect Join combines the records of two queries together and returns only rows that appear in
both queries.
INTERSECT
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;