Documente Academic
Documente Profesional
Documente Cultură
Overview
This document demonstrates the linking process in Crystal Reports (CR) 7 and
later. This document discusses linking for PC-type databases, ODBC linking and
frequently asked questions.
The linking processes are based on matching records, from a parent table (left
table) to records in a child table (right table). The match is based on whether the
value in the link field in the source record is identical to the value of the link
field in the searched records. The linking process is a form of record selection.
Contents
INTRODUCTION ............................................................................................ 3
GENERAL LINKING OVERVIEW ..................................................................... 3
Link Relationships........................................................................................3
One-to-one ......................................................................................................... 4
One-to-Many...................................................................................................... 4
ODBC LINKING .......................................................................................... 4
What is ODBC? ...........................................................................................4
SQL Join Types ODBC connection ...........................................................4
Equal [=] join..................................................................................................... 5
Left Outer [=(+), *=] join .................................................................................. 5
Right Outer [(+)=, =*] join ................................................................................ 6
Greater Than [>] join ......................................................................................... 6
Less Than [<] join.............................................................................................. 7
Greater Than Or Equal [>=] join........................................................................ 7
Less Than Or Equal [<=] join ............................................................................ 8
Not Equal [!=, <>] join ...................................................................................... 8
HOW MULTIPLE TABLES AND MULTIPLE JOINS BETWEEN TABLES AFFECT THE
RECORD SET RETURNED .............................................................................. 9
Join Types Between Two Tables ..................................................................9
Two equal joins between two tables...........................................................10
Two Left Outer joins between two tables ...................................................10
Multiple Tables ..........................................................................................10
Equal Joins between tables A, B, and C........................................................... 11
Equal Join between tables A and B and Left Outer join between tables B and C
......................................................................................................................... 11
Left Outer Join between all three tables........................................................... 12
Left Outer Join between tables A and B and Equal Join between tables B and C
......................................................................................................................... 12
4/6/2004 1:49 PM Copyright 2002 Crystal Decisions, Inc. All Rights Reserved. Page 1
Crystal Reports Table Linking in Crystal Reports
4/6/2004 1:49 PM Copyright 2002 Crystal Decisions, Inc. All Rights Reserved. Page 2
scr_pclinking.pdf
Crystal Reports Table Linking in Crystal Reports
Introduction
Linking tables is not required when reporting off only one database table.
However, it is often necessary to report off more than one database table.
To report off more than one table in a single report, the tables MUST be linked.
The linking process defines the relationship between the database tables. The
relationship compares records between the tables.
The available linking options in Crystal Reports differ with the type of
connection to the database. In other words, the linking options are different for
ODBC and native (direct) connections.
Using tables from Xtreme.mdb, the sample database that is shipped with Crystal
Reports, select the Orders table and the Customers table to create a new report.
These tables can be linked to match all the orders with the customer.
This is achieved by linking based on a field that is common to both tables. When
linking tables, the link starts from one table to another. The from table is
considered the parent table (also referred to as the primary table or the left table)
and the to table is the child table (also referred to as the lookup table or the right
table).
Which table do you use as the parent table? This depends on the type of link
relationship that is required to return the correct records.
Link Relationships
When records are linked from one table to another table they will have a
relationship type of either:
One-to-one
One-to-many
NOTE The term "link" in Crystal Reports is equivalent to the term "relationship" in Microsoft Access.
4/6/2004 1:49 PM Copyright 2002 Crystal Decisions, Inc. All Rights Reserved. Page 3
scr_pclinking.pdf
Crystal Reports Table Linking in Crystal Reports
One-to-one
In a one-to-one relationship between records in two linked tables, for every
record in the primary table (left) there is only one matching record in the lookup
table (right).
For example, the Xtreme.mdb database contains the Product and Product_Type
tables. These two tables are linkable on the common Product Type ID field. For
each Product Type ID there is only one Product Type Name. This is considered
to be a one-to-one relationship.
One-to-Many
In a one-to-many relationship between fields in two linked tables, for every
record in the primary table (left), there can be multiple matching records in the
lookup table (right).
For example, the Xtreme.mdb contains the Orders and the Orders_Detail tables.
The two tables are linked by the Order ID field. For each Order ID there can be
multiple Order IDs in the Orders Details table. This is considered to be a one-
to-many relationship.
ODBC Linking
What is ODBC?
ODBC stands for Open Database Connectivity. It is a standard developed by
Microsoft through which different types of data can be accessed. An application
and a database driver communicate through a set of ODBC files known as the
ODBC layer. The ODBC layer uses a common language, SQL (Structured
Query Language) to communicate to any type of database.
Equal Join
Left Outer
Right Outer
Greater Than
Less Than
Greater Than or Equal
Less Than or Equal
Not Equal
4/6/2004 1:49 PM Copyright 2002 Crystal Decisions, Inc. All Rights Reserved. Page 4
scr_pclinking.pdf
Crystal Reports Table Linking in Crystal Reports
Notice that there are no blue fruits or vegetables in the second sample table
therefore, no matching records are returned for 3 Blue. Also 5 Banana is not
returned because there is no matching record in the first sample table.
Notice that 3 blue from the left hand table is now returned. 5 Banana from the
right hand table is still not returned, as the record is not from the left table.
4/6/2004 1:49 PM Copyright 2002 Crystal Decisions, Inc. All Rights Reserved. Page 5
scr_pclinking.pdf
Crystal Reports Table Linking in Crystal Reports
Notice that 5 banana is returned because it is in the right hand table but 3 blue is
not included in the record set because there is no matching record in the right
table.
Records are returned when the Color ID of the LEFT table is greater than the
Color ID of the RIGHT table.
4/6/2004 1:49 PM Copyright 2002 Crystal Decisions, Inc. All Rights Reserved. Page 6
scr_pclinking.pdf
Crystal Reports Table Linking in Crystal Reports
Records are returned when the Color ID of the LEFT table is LESS than the
Color ID of the RIGHT table.
Records are returned when the Color ID of the LEFT table is greater than or
equal to the color ID of the RIGHT table.
4/6/2004 1:49 PM Copyright 2002 Crystal Decisions, Inc. All Rights Reserved. Page 7
scr_pclinking.pdf
Crystal Reports Table Linking in Crystal Reports
Records are returned when the Color ID of the LEFT table is LESS than or
Equal to the Color ID of the RIGHT table.
Records are returned when the Color ID of the LEFT table is NOT EQUAL to
the Color ID of the RIGHT table.
4/6/2004 1:49 PM Copyright 2002 Crystal Decisions, Inc. All Rights Reserved. Page 8
scr_pclinking.pdf
Crystal Reports Table Linking in Crystal Reports
NOTE The most common join types are the Equal and Left Outer.
4/6/2004 1:49 PM Copyright 2002 Crystal Decisions, Inc. All Rights Reserved. Page 9
scr_pclinking.pdf
Crystal Reports Table Linking in Crystal Reports
NOTE If there are two links between two tables, when the join type for one link is changed, the
join type for the other link will automatically change to the same type of link. It is not
possible to have two different kinds of joins between two tables.
Multiple Tables
As more tables and more links are added, it becomes more and more difficult to
understand how the linking will affect the record set.
The following example examines the record sets returned when there is linking
between three tables.
4/6/2004 1:49 PM Copyright 2002 Crystal Decisions, Inc. All Rights Reserved. Page 10
scr_pclinking.pdf
Crystal Reports Table Linking in Crystal Reports
For example, with the above three-table scenario, records are returned that have
matching values in all three tables.
Notice that green and lime from the first two tables are not returned because
there is no match in the third table.
Notice that now the green lime and broccoli are returned even though there is no
matching food in table C.
Record Set Returned From Equal Join Between A and B and a Left Outer
Join Between B and C
Colors ID Color Colors ID Fruit_Veg Fruit_Veg_ID Fruit_veg ID Food
1 Orange 1 Orange 1 1 Marmalade
2 Red 2 Apple 2 2 Apple crumble
2 Red 2 Strawberry 3 3 Strawberry shortcake
4 Green 4 Lime 4
4 Green 4 Broccoli 5
4/6/2004 1:49 PM Copyright 2002 Crystal Decisions, Inc. All Rights Reserved. Page 11
scr_pclinking.pdf
Crystal Reports Table Linking in Crystal Reports
Notice that 3 blue is returned even though there is no match in either table B or
C.
Notice that banana and banana cream pie is NOT returned. This is because
table A is the Driving table and the Left Outer join between Table A and Table
B excludes this data.
Record Set Returned From Left Outer Joins Between Tables A, B and C
Colors ID Color Colors ID Fruit_Veg Fruit_Veg_ID Fruit_veg ID Food
1 Orange 1 Orange 1 1 Marmalade
2 Red 2 Apple 2 2 Apple crumble
2 Red 2 Strawberry 3 3 Strawberry shortcake
3 Blue
4 Green 4 Lime 4
4 Green 4 Broccoli 5
This means that the Microsoft driver is unable to interpret the SQL for this
linking scenario. In order to create a report that uses this linking, one would
have to create a main report based on table A and then a linked subreport that is
based on tables B and C. The linked subreport will give you the equivalent of a
left outer join link.
4/6/2004 1:49 PM Copyright 2002 Crystal Decisions, Inc. All Rights Reserved. Page 12
scr_pclinking.pdf
Crystal Reports Table Linking in Crystal Reports
NOTE Connecting Natively to an SQL- type database using SQL-type native (direct) database
drivers defaults to an Equal join.
Examples of these client/server databases are:
DB2, Informix, Lotus Notes, Oracle, Pervasive, SQL Server
Crystal Reports (CR) creates virtual records by taking each record containing a
specific value in the link field in table A and connecting it to a corresponding
record containing an identical value in the link field in table B.
In this case the link field is EmpID. When linking from Table A to Table B,
using the data in this example, the following record set will be returned to the
report:
Record Set Returned From Linking Two Identical Fields From Table A
and Table B
EmpID EmpName EmpID Project Amount
1 Kim Smith 1 Project 1 1,000.00
2 Linda Johnson 2 Project 3 5,000.00
2 Linda Johnson 2 Project 4 10,000.00
2 Linda Johnson 2 Project 3 10,000.00
Crystal Reports (CR) concatenates the first record from the parent table (A) to
each record containing a link field of identical value in the child table (B).
When no further matches are found in the child table, CR moves to the next
record in the parent table. This process is repeated for all the remaining records
in the parent table.
Each record from the parent table is treated as a unique record regardless of
whether it really is. In the example above, EmpID 1, from the parent table has
only one corresponding match in the child table (B). However, EmpID2 has
3 matches. Consequently there are 3 virtual records created for Linda Johnson,
4/6/2004 1:49 PM Copyright 2002 Crystal Decisions, Inc. All Rights Reserved. Page 13
scr_pclinking.pdf
Crystal Reports Table Linking in Crystal Reports
EmpID 2, when only one source record really exists in the actual database (in
Table A). This is the result of concatenation of records from the parent table to
the child table.
A -> B ->C
The three sample database tables are used in the following linking examples.
Crystal Reports creates virtual records that follow along every valid link on
EmpID that matches in all 3 tables. The following would be the record set
returned to the report in this type of relationship:
Record Set Returned From Linking Three Identical Fields From Table A,
Table B and Table C
Table A Table B Table C
EmpID EmpName EmpID Project Amount EmpID Class
1 Kim Smith 1 Project 1 1,000.00 1 Class 3
2 Linda Johnson 2 Project 2 5,000.00 2 Class 4
2 Linda Johnson 2 Project 2 5,000.00 2 Class 5
2 Linda Johnson 2 Project 2 5,000.00 2 Class 6
2 Linda Johnson 2 Project 2 5,000.00 2 Class 7
2 Linda Johnson 2 Project 3 10,000.00 2 Class 4
2 Linda Johnson 2 Project 3 10,000.00 2 Class 5
2 Linda Johnson 2 Project 3 10,000.00 2 Class 6
2 Linda Johnson 2 Project 3 10,000.00 2 Class 7
2 Linda Johnson 2 Project 4 10,000.00 2 Class 4
2 Linda Johnson 2 Project 4 10,000.00 2 Class 5
2 Linda Johnson 2 Project 4 10,000.00 2 Class 6
2 Linda Johnson 2 Project 4 10,000.00 2 Class 7
4/6/2004 1:49 PM Copyright 2002 Crystal Decisions, Inc. All Rights Reserved. Page 14
scr_pclinking.pdf
Crystal Reports Table Linking in Crystal Reports
Since there is only one occurrence of EmpID 1 in Tables A, B, and C, the total
number of virtual records created is 1 x 1 x 1 = 1. Since there are one
occurrence of EmpID 2 in Table A, three occurrences in Table B, and 4
occurrences in Table C, the total number of virtual records created for EmpID 2
is 1 x 3 x 4 = 12.
NOTE Depending on how you link tables/databases, and the number of valid links between
them, the number of detail records returned to the report can be much greater than the
total number of actual records contained in the tables/databases. As in the example
above, there are a total of nine (9) records in all three tables, but on the report, Crystal
Reports would return thirteen (13) detail records.
The A to B, A to C Relationship
This section demonstrates record set returned from the linking of three tables
using an A to B, A to C relationship.
There are three options available when setting up the linking relationship in
Crystal Reports:
CAUTION This is a relationship available only to PC-type databases, not SQL- type databases using
a native connection.
Otherwise, a blank record will be appended to the virtual record. In the event
that a blank is found from both look-up tables, the resultant virtual record will
not be included and the read process for the next source record from Table A,
initiated.
The number of virtual records created for a given source record is equal to the
number of records in the table that has the maximum number of records
matching the source record. In this example, Table C contains the maximum
4/6/2004 1:49 PM Copyright 2002 Crystal Decisions, Inc. All Rights Reserved. Page 15
scr_pclinking.pdf
Crystal Reports Table Linking in Crystal Reports
After reading through Table B completely, Crystal Reports looks for matches to
the source record in Table C. It appends a copy of the source record to a blank
field, and then appends a match found in Table C to create the virtual record.
This is done for each match found in Table C.
The total number of virtual records created per source record is determined by
summing together the number of matched records for the source record in each
table.
scr_pclinking.pdf
Crystal Reports Table Linking in Crystal Reports
Once all matching records from Table C have been exhausted, Crystal Reports
returns to Table B to select the next record that matches the source record,
concatenates the two records, and appends a copy to each matching record from
Table C.
With this lookup option, the number of virtual records created equals the
number of matching records in Table A x matches in Table B x matches in
Table C. In this example, Linda Johnson, there are 1 x 3 x 4 = 12 virtual records.
Why does the option No Lookup yield the same results as the option Look up
all combinations of the two?
4/6/2004 1:49 PM Copyright 2002 Crystal Decisions, Inc. All Rights Reserved. Page 17
scr_pclinking.pdf
Crystal Reports Table Linking in Crystal Reports
A native connection requires less setup and may be faster because it connects
directly to the database.
With an ODBC connection, in order to link fields, they must be of the same data
type and length.
With Native Connections, only indexed fields can be linked. These are
identifiable by the colored markers beside them.
All link warnings were ignored and the report still works. Is
this OK?
You should not ignore the link warnings. The report may run but may be
unstable and/or return erroneous or no data.
Smart linking links fields of the same name and same data type and length.
Smart linking is not necessarily the correct linking required for your desired
result set.
4/6/2004 1:49 PM Copyright 2002 Crystal Decisions, Inc. All Rights Reserved. Page 18
scr_pclinking.pdf
Crystal Reports Table Linking in Crystal Reports
Self-serve Support:
http://support.crystaldecisions.com/
Email Support:
http://support.crystaldecisions.com/support/answers.asp
Telephone Support:
http://www.crystaldecisions.com/contact/support.asp
4/6/2004 1:49 PM Copyright 2002 Crystal Decisions, Inc. All Rights Reserved. Page 19
scr_pclinking.pdf