Using the Dynamic JOIN Command

With the dynamic JOIN command, you can reference two or more related tables (or non- FOCUS data sources) in a single FOCUS report request. The data structures remain physically separate, but FOCUS treats them as a single logical structure.

In this section:

Constructing a Single-field Dynamic Equijoin

Constructing a Multi-field Dynamic Equijoin

Constructing a Conditional Join

Reference:

Preserving Virtual Fields During Join Parsing

Two types of join are available: the equijoin and the conditional (WHERE-based join).

The terms primary key and foreign key refer to the common columns that relate host and cross-referenced tables in an equijoin.

Although the run-time effect of the dynamic join is very similar to that of the embedded join discussed in Multi-Table Structures, the dynamic join is easier to construct since it does not require a separate Master File and Access File. You can create a dynamic join on an as-needed basis.

Note: A TABLE request that references a dynamically joined structure generates SQL join predicates for all segments in the subtree that starts from the root segment. Multi-table Master Files do not necessarily generate these predicates; in a multi-table structure, the subtree effectively begins with the highest referenced segment. This difference may cause identical TABLE requests to produce different reports when run against a dynamic join structure and a multi-table Master File that represent the same tree structure.

The dynamic join is limited to FOCUS read-only operations (for example, TABLE, GRAPH, and the MODIFY LOOKUP facility). Embedded joins in a Master File support both read-only and read-write operations.

FOCUS joins can be unique or non-unique. The difference between the two depends on the cross-referenced relation and its foreign key values:

Note:

With either type of join, some rows in the host table may lack corresponding rows in the cross-referenced table. In a report request, such a retrieval path is called a short path.

When a report omits host rows that lack corresponding cross-referenced rows, the join is called an inner join. When a report displays all matching rows plus all rows from the host file that lack corresponding cross-referenced rows, the join is called a left outer join.

Sometimes FOCUS passes responsibility for a join to the RDBMS. The OPTIMIZATION setting is one factor in determining whether a join is optimized; other factors depend on the specific elements in the report request (see The Data Adapter Optimizer). In order to understand join behavior, you must know whether optimization is enabled or disabled for a particular report request. The SQLAGGR trace gives you that information (see Tracing Data Adapter Processing).

For both embedded and dynamic joins, factors that determine whether a report includes short path rows are the type of join, the FOCUS SET ALL command, and whether FOCUS or the RDBMS is handling the join. Subsequent sections describe how these factors interact. SET ALL is described in The SET ALL Command.


Top of page

Reference: Preserving Virtual Fields During Join Parsing

By default, a JOIN command clears all DEFINE FILE commands for the host data source and the joined structure. Two methods are available for preserving virtual fields during join parsing: the KEEPDEFINES parameter and the DEFINE FILE SAVE and RETURN commands. For complete information, see your FOCUS documentation.


Top of page

Constructing a Single-field Dynamic Equijoin

A single-field dynamic equijoin is a join based on relating values in one column of the host and cross-referenced tables.

How to:

Construct a Single- Field Dynamic Equijoin

Example:

Using a Single-Field Dynamic Equijoin

Implementing a Single-Field Dynamic Equijoin


Top of page

Syntax: How to Construct a Single-Field Dynamic Equijoin

JOIN fielda1 [WITH rfield] IN hostfile [TAG tag1] TO [ALL]
fieldb1 IN crfile [TAG tag2] AS joinname
[END]

where:

fielda1
Is a field in the host file or a DEFINE field that shares values and format with fieldb1 in the cross-referenced file.

WITH rfield
Use only if fielda1 is a DEFINE field; associates the DEFINE field with the segment location of a real field (rfield) in the host file.

hostfile
Is the name of the host file. Use this name in subsequent TABLE requests on the joined structure.

tag1
Is the tag name for the host file, a one- to eight-character table name qualifier for field and alias names in the host file. The tag name for the host must be the same in all the JOIN commands for a join structure. The tag name may not be the same as any table name in the structure.

ALL
Indicates that the host and cross-referenced files participate in a one-to-many or many-to-many relationship. That is, for any value of the join field in the host file (fielda1), there may be more than one corresponding instance of that value for the join field in the cross-referenced file (fieldb1). In FOCUS terminology, this is known as a non-unique or multiple join.

Note:

The use of the ALL parameter does not disable optimization. Do not confuse this ALL parameter with the SET ALL command discussed in The SET ALL Command.

Omitting the ALL parameter indicates a unique join. Omit the ALL parameter only when each row in the host file has, at most, one corresponding row in the cross-referenced file. The unique join is a FOCUS concept; the RDBMS makes no distinction between unique and non-unique situations when it processes a join.

fieldb1
Is a field in the cross-referenced file whose values and format can match that of fielda1.

crfile
Is the name of the cross-referenced file.

tag2
Is the tag name for the cross-referenced file, a one- to eight-character table name qualifier for field and alias names in the cross-referenced file. The tag name may not be the same as any table name in the structure. In a recursive join structure, if no tag name is provided, FOCUS prefixes all fieldnames and aliases with the first four characters of the join name.

joinname
Assigns an internal name to the join structure, up to eight characters in length. Joinname also provides a unique prefix for fieldnames participating in a recursive join.

You can clear the join name when you no longer need this join (see JOIN CLEAR). Do not specify joinname as the filename in subsequent TABLE FILE requests; use hostfile.

END
Required when the JOIN statement is longer than one line; terminates the statement.


Top of page

Example: Using a Single-Field Dynamic Equijoin

JOIN EID IN EMPINFO TAG FILE1 TO ALL PAYEID IN PAYINFO TAG FILE2 AS JOIN1

You can join up to 16 structures to create a FOCUS view of the data. The joined structure remains in effect for the duration of the FOCUS session or until you clear the join name using the JOIN CLEAR command (see JOIN CLEAR). The data adapter instructs the RDBMS to perform a join based on the smallest subtree of referenced tables (from the root) required to satisfy the request.

Note: For a join to be optimized, it cannot involve more than 15 tables or views, the RDBMS limit for an SQL statement.

Each cross-referenced table must have at least one data field in common with its host file. Fixed sequential, ISAM, VSAM, IMS, CA-IDMS/DB, CA-Datacom/DB®, ADABAS®, Teradata, MODEL 204®, Oracle, and RDBMS tables and views can be both host files and cross-referenced files in any combination. You can also join these files to all segments of a FOCUS data source by using FOCUS database indexed fields.

If the DB2 Distributed Data Facility is installed:

The following chart lists some of the more common combinations available with the FOCUS dynamic JOIN command and restrictions on their use. Consult your FOCUS documentation on creating reports for additional data source combinations and examples of dynamic joins. In the chart, SQL refers to DB2, DB2 for VM, Teradata, IDMS SQL, or Oracle tables and views:

From

To

Special Rules That May Apply

SQL

SQL

Joined fields must be of the same data type. For efficient retrieval, their lengths should also be equivalent. Use of indexed fields for the host and cross-referenced fields is recommended, but the RDBMS uses the indexes only if both the data types and lengths are equal.

SQL

FOCUS

Must join to an indexed field (FIELDTYPE=I). Joined fields must have common data type and length.

SQL

IMS

XMI/DLI, XMI/MVS/IMS, or IMS Data Adapter must be installed. The DL/I join field must be a key field in the root segment of the data source; it can be a primary or secondary index. Join fields must have a common data type and length.

SQL

VSAM

For a unique join, the VSAM join field must be a full primary key. For a non-unique join, the join field can be an initial subset of the primary key. Joined fields must have a common data type and length.

SQL

QSAM

QSAM file must be sorted by its join field. Joined fields must have a common data type and length.

VSAM

SQL

Joined fields must have a common data type and length.

QSAM

SQL

Joined fields must have a common data type and length.

Note:


Top of page

Example: Implementing a Single-Field Dynamic Equijoin

The following examples illustrate the use of the dynamic JOIN command. Each example specifies a non-unique join and presents an equivalent SQL request for comparison purposes. Both forms of the request, FOCUS and SQL, return the same result.

Note:

The first example prints each employee's full name and courses taken. Since the employee may have taken more than one course, the example specifies the FOCUS non-unique join.

Employee information is stored in the DB2 table EMPINFO and is represented by the fields LAST_NAME and FIRST_NAME. Course information is stored in the DB2 table COURSE and is represented by the CNAME field.

set traceuser = on
set traceoff = all
set traceon = stmtrace//client

join emp_id in empinfo tag file1 to all who in course tag file2 as j1
table file empinfo
print cname
by last_name by first_name
end

SELECT T1.EID,T1.LN,T1.FN,T2.COURSE_NAME FROM
"USER1"."EMPINFO" T1,"USER1"."COURSE" T2 WHERE (T2.EMP_NO =
T1.EID) ORDER BY T1.LN,T1.FN FOR FETCH ONLY;

The SQL request references both tables in the FROM clause and as a condition (or join predicate) of the WHERE clause. The BY phrases translate to the ORDER BY phrase. The PRINT command translates as SELECT.

The next example illustrates a screening test that lists the employees who have taken either the Advanced or Internals course.

set traceuser = on
set traceoff = all
set traceon = stmtrace//client

join who in course tag file1 to all emp_id in empinfo tag file2 as j2
table file course
print cname
by last_name by first_name
where cname is 'advanced' or 'internals'
end

SELECT T1.COURSE_NAME,T1.EMP_NO,T2.LN,T2.FN FROM
"USER1"."COURSE" T1,"USER1"."EMPINFO" T2 WHERE (T2.EID =
T1.EMP_NO) AND (T1.COURSE_NAME IN('ADVANCED','INTERNALS'))
ORDER BY T2.LN,T2.FN FOR FETCH ONLY;

The SQL request references both tables in the FROM clause and in a join predicate in the WHERE clause (along with additional screening conditions). The BY phrases translate to the ORDER BY phrase.


Top of page

Constructing a Multi-field Dynamic Equijoin

You can construct a dynamic join based on multiple fields from the host file and cross-referenced files. Separate the participating fieldnames with the keyword AND.

How to:

Construct a Multi- Field Dynamic Equijoin

Example:

Using a Multi-field Dynamic Join


Top of page

Syntax: How to Construct a Multi-Field Dynamic Equijoin

JOIN fielda1 AND fielda2 IN hostfile [TAG tag1] TO [ALL]
fieldb1 AND fieldb2 IN crfile [TAG tag2] AS joinname
[END]

where:

fielda1
Is a field in the host file that shares values and format with fieldb1 in the cross-referenced file.

AND fielda2
Is a field in the host file that shares values and format with fieldb2 in the cross-referenced file.

hostfile
Is the name of the host file. Use this name in subsequent TABLE requests on the joined structure. The host file can be any type of database or table. See Constructing a Single- field Dynamic Equijoin for possible join combinations.

tag1
Is the tag name for the host file, a one- to eight-character table name qualifier for field and alias names in the host file. The tag name for the host must be the same in all the JOIN commands for a join structure. The tag name may not be the same as any table name in the structure

ALL
Indicates that the host and cross-referenced files participate in a one-to-many or many-to-many relationship. That is, for any instance of the join fields in the host file (fielda1, fielda2), there may be more than one corresponding instance of the join fields in the cross-referenced file (fieldb1, fieldb2). In FOCUS terminology, this is known as a non-unique or multiple join.

Note:

The use of the ALL parameter does not disable optimization. Do not confuse this ALL parameter with the SET ALL command discussed in The SET ALL Command.

Omitting the ALL parameter indicates a unique join. Omit the ALL parameter only when each row in the host file has, at most, one corresponding row in the cross-referenced file. The unique join is a FOCUS concept; the RDBMS makes no distinction between unique and non-unique situations when it processes a join.

fieldb1
Is a field in the cross-referenced file whose values and format can match that of fielda1.

AND fieldb2
Is a field in the cross-referenced file whose values and format can match that of fielda2.

crfile
Is the name of the cross-referenced file.

tag2
Is the tag name for the cross-referenced file, a one- to eight-character table name qualifier for field and alias names in the cross-referenced file. The tag name may not be the same as any table name in the structure. In a recursive join structure, if no tag name is provided, FOCUS prefixes all fieldnames and aliases with the first four characters of the joinname.

joinname
Assigns an internal name to the join structure, up to eight characters in length. It also provides a unique prefix for fieldnames participating in a recursive join.

You can clear the join name when you no longer need this join (see JOIN CLEAR). Do not specify joinname as the filename in subsequent TABLE FILE requests; use hostfile.

END
Required when the JOIN statement is longer than one line; terminates the statement.

For a multi-field join, the joined fields from each table must be equal in number, data type, and field length. The full set of common fields must reside in both the host and cross-referenced tables.

You can join a maximum of 16 fields from a host file to 16 fields in a cross-referenced file. Each multi-field JOIN command counts as one join toward the FOCUS maximum of 16 concurrent joins.

Note: For a join to be optimized, it cannot involve more than 15 tables or views, the RDBMS limit for an SQL statement.


Top of page

Example: Using a Multi-field Dynamic Join

For example, two RDBMS tables, EMPINFO and COURSE1, have first and last name fields. In the EMPINFO Master File, LAST_NAME and FIRST_NAME have field formats A15 and A10:

FILENAME=EMPINFO  ,SUFFIX=SQLDS,$

SEGNAME=EMPINFO ,SEGTYPE=S0,$
FIELD=EMP_ID ,ALIAS=EID ,USAGE=A9 ,ACTUAL=A9,$
FIELD=LAST_NAME ,ALIAS=LN ,USAGE=A15 ,ACTUAL=A15,$
FIELD=FIRST_NAME ,ALIAS=FN ,USAGE=A10 ,ACTUAL=A10,$
FIELD=HIRE_DATE ,ALIAS=HDT ,USAGE=YMD ,ACTUAL=DATE,$
FIELD=DEPARTMENT ,ALIAS=DPT ,USAGE=A10 ,ACTUAL=A10,
MISSING=ON,$
FIELD=CURRENT_SALARY ,ALIAS=CSAL ,USAGE=P9.2 ,ACTUAL=P4,$
FIELD=CURR_JOBCODE ,ALIAS=CJC ,USAGE=A3 ,ACTUAL=A3,$
FIELD=ED_HRS ,ALIAS=OJT ,USAGE=F6.2 ,ACTUAL=F4,
MISSING=ON,$
FIELD=BONUS_PLAN ,ALIAS=BONUS_PLAN ,USAGE=I4 ,ACTUAL=I4,$
FIELD=HIRE_DATE_TIME ,ALIAS=HDTT ,USAGE=HYYMDm ,ACTUAL=HYYMDm,$
FIELD=HIRE_TIME ,ALIAS=HT ,USAGE=HHIS ,ACTUAL=HHIS,$

In the COURSE1 Master File, LAST_NAME and FIRST_NAME have the same field formats, A15 and A10:

FILE=COURSE1     ,SUFFIX=SQLDS, $
SEGNAME=SEG01 ,SEGTYPE=S0, $
FIELDNAME =FOCLIST ,FOCLIST ,I5 ,I4 ,$
FIELDNAME =CNAME ,COURSE_NAME ,A15 ,A15 ,$
FIELDNAME =LAST_NAME ,LN ,A15 ,A15 ,$
FIELDNAME =FIRST_NAME ,FN ,A10 ,A10 ,$
FIELDNAME =GRADE ,GRADE ,A1 ,A1 ,
MISSING=ON, $
FIELDNAME =YR_TAKEN ,YR_TAKEN ,A2 ,A2 ,$
FIELDNAME =QTR ,QUARTER ,A1 ,A1 ,$

Note: File Descriptions and Tables, does not include the COURSE1 Master File. To reproduce the COURSE1 Master File, extract the fields from the ECOURSE Master File and use the HOLD FORMAT SQL option explained in Creating Tables Using the HOLD Command.

To create the multi-field join, list both fields for each table with the keyword AND:

JOIN LAST_NAME AND FISRT_NAME IN EMPINFO TAG FILE1
TO ALL LAST_NAME AND FIRST_NAME IN COURSE1 TAG FILE2 AS J1
END

When a report request references the multi-field dynamic equijoin, the data adapter generates an SQL SELECT statement to satisfy the request:

set traceuser = on
set traceoff = all
set traceon = stmtrace//client

table file empinfo
print last_name first_name cname
end

SELECT T1.LN,T1.FN,T2.COURSE_NAME FROM "USER1"."EMPINFO" T1,
"USER1"."COURSE1" T2 WHERE (T2.LN = T1.LN) AND (T2.FN = T1.FN) FOR
FETCH ONLY;

The SQL SELECT statement implements two joins as conditions (or predicates) of the WHERE clause.


Top of page

Constructing a Conditional Join

The conditional join lets you specify conditions other than equality between fields for relating two tables.

How to:

Construct a Conditional Join

Example:

Using a Conditional Join

The conditional JOIN command supports FOCUS, relational, VSAM, fixed-format sequential, ADABAS, and IMS data sources. Because each data source differs in its ability to handle complex WHERE criteria, the optimization of the WHERE-based JOIN syntax differs depending on the specific data sources involved in the join and the complexity of the WHERE criteria.


Top of page

Syntax: How to Construct a Conditional Join

JOIN FILE from_file AT from_field [TAG from_tag] [WITH fieldname]
TO [ALL|ONE]
FILE to_file AT to_field [TAG to_tag]
[AS as_name]
[WHERE expression1 ;
WHERE expression2 ;
... ;]
END

where:

from_file
Is the host Master File.

from_field
Is the field name in the host Master File whose segment will be joined to the cross- referenced data source. It can be any field in the segment. It must reside in the lowest level segment that will be referenced.

from_tag
Is the optional tag name that is used as a unique qualifier for fields and aliases in the host data source.

fieldname
Is a real field name used to assign a segment location for a virtual field. Required when issuing a DEFINE field-based WHERE-based JOIN.

ALL
Describes a one-to-many relationship between the from_file and to_file.

ONE
Describes a one-to-one relationship between the from_file and to_file.

Note:

If you specify a unique join when the relationship between the host and cross- referenced files is one-to-many, the results will be unpredictable.

to_file
Is the cross-referenced Master File.

to_field
Is the join field name in the cross-referenced Master File. It can be any field in the segment.

to_tag
Is the optional tag name that is used as a unique qualifier for fields and aliases in the cross-referenced data source.

as_name
Is the name associated with the JOIN.

expression1, expression2
Are any expressions valid in a DEFINE FILE command. All of the fields used in all of the expressions the expressions must lie on a single path.

Note: Single line JOIN syntax is not supported. The END command is required.

To issue a DEFINE-based conditional join, the KEEPDEFINES setting must be ON. You then must create all virtual fields before issuing the join. This differs from traditional DEFINE- based joins in which the virtual field is created after the JOIN command is issued. In addition, a virtual field can be part of the JOIN syntax or WHERE criteria. For a discussion of the KEEPDEFINES setting, see your FOCUS documentation.


Top of page

Example: Using a Conditional Join

set traceuser = on
set traceoff = all
set traceon = stmtrace//client

join file empinfo at emp_id tag e1
to all file payinfo at payeid tag p1 as jw
where emp_id eq payeid;
where (dat_inc - hire_date) gt 0;
where (dat_inc - hire_date) lt 365;
end

table file empinfo
print current_salary
compute diff/i5 = dat_inc - hire_date;
end

For DB2, the following SQL is generated:

     SELECT T1."EID",T1."HDT",T1."CSAL",T2."EID",T2."DI" FROM
"USER1"."EMPINFO" T1,"USER1"."PAYINFO" T2 WHERE (T2."EID" =
T1."EID") AND ((DAYS(T2."DI") - DAYS(T1."HDT")) > 0) AND
((DAYS(T2."DI") - DAYS(T1."HDT")) < 365) FOR FETCH ONLY;

The SQL request references both tables in the FROM clause and incorporates the conditions specified in the JOIN command as predicates of the WHERE clause. The PRINT command translates as SELECT.


Information Builders