Академический Документы
Профессиональный Документы
Культура Документы
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
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)
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 for Joined Table 3 would be: Select * from Personal_Data_Table join Citizenship Data on ID = ID;
pg. 3
Princeton University
Table A EmplID Country 123 USA 124 USA 125 USA 130 Mexico 131 Mexico 132 Mexico 140 Mexico Total: 7 Records
EmplID 120 121 123 124 125 130 133 140 141 142
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.
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 EmplID Country 123 USA 124 USA 125 USA 130 Mexico 131 Mexico 132 Mexico 140 Mexico Total: 7 Records
EmplID 120 121 123 124 125 130 133 140 141 142
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
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).
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 EmplID Country 123 A 124 A 125 A 130 B 131 B 132 B 140 B Total: 7 Records
EmplID 120 121 123 124 125 130 133 140 141 142
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 124 124 124 124 124 etc Total: 70 Records A B B C C C
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)
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)
pg. 10
Princeton University
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)
pg. 11
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)
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.
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.
pg. 14
Princeton University
pg. 15
Princeton University
pg. 16
Princeton University Steps: Create two queries. 1. 2. 3. 4. Open Report Studio. Select a List type report. Save this report as Join Personal & Job Data. Mouse over the Query Explorer and click on yellow Queries icon. Click once on the blue Query 1 icon. In the Properties area, in the Name option, rename Query1 to PersonalQuery.
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.
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.
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.
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).
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.
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 ID Last Name First Name Street Address 1 City State Postal Code
JobQuery ID Full Name Last Name First Name Middle Name Dept ID Dept Name Salary
These are the fields which will be used in the PersonalJobQuery to populate the list report, on the Report Page.
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.
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.
pg. 24
Princeton University
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
pg. 25
Princeton University
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.
pg. 27
Princeton University
pg. 28
Princeton University
pg. 29