JOIN Syntax and Techniques Noreen Redden Information Builders Copyright 2011, Information Builders. Slide 1
JOIN Syntax and Techniques Overview When To JOIN Type Of JOINs JOIN Techniques JOIN Syntax Joining Files In The Synonym Considerations Review Copyright 2011, Information Builders. Slide 2
When To JOIN Copyright 2011, Information Builders. Slide 3
JOIN Syntax and Techniques When To JOIN Files Data Is Stored In Several Locations Different Servers Different Databases Different Tables A Relationship Exists Between The Two Sources A Common Key Exists Between The Two Sources If the fields/columns are the same, but different years, etc., use MORE or Concatenation To Update multiple files with different fields/columns, use MAINTAIN or COMBINE with MODIFY Copyright 2011, Information Builders. Slide 4
Types of JOINs Copyright 2011, Information Builders. Slide 5
JOIN Syntax and Techniques Types Of JOINs Data Source Dependent Non Keyed (FIX Format) Keyed FOCUS VSAM Relational Cross Reference Source Determines The Rules Oracle to OracleFOCUS to FIX FOCUS to FOCUSFIX to FOCUS Oracle to FOCUS FOCUS to Oracle SQL Server to Oracle Copyright 2011, Information Builders. Slide 6
JOIN Syntax and Techniques Types Of JOINs JOIN Behavior INNER Only Records That Occur In Host AND Cross Reference Are Returned LEFT OUTER All Records From The Host Are Processed regardless of a corresponding Record In The Cross Reference Non Relational Considerations UNIQUE – Return Only One Matching Record From The Cross Reference MULTIPLE – More Than One Record May exist in the Cross Reference Copyright 2011, Information Builders. Slide 7
JOIN Techniques Copyright 2011, Information Builders. Slide 8
JOIN Syntax and Techniques JOIN Techniques JOIN Statements Explicitly Coded In The Procedure Gives Flexibility To End Users Requires Knowledge of Data And Data Relationships Can Join On Real Field Or Defined Field (DEFINE BASED) Can Issue A Specific Join Predicate (WHERE BASED) Cluster Synonyms Uses Syntax In The Synonym Allows Better Control Copyright 2011, Information Builders. Slide 9
JOIN Syntax Copyright 2011, Information Builders. Slide 10
JOIN Syntax and Techniques JOIN Syntax Items In […] Are Optional If LEFT_OUTER Is Omitted, An INNER JOIN Is Implied If UNIQUE or MULTIPLE Is Omitted, UNIQUE Is Implied AS asname Should Only Be Omitted If there Is only one JOIN statement. Always using an AS name Is good practice END Is only required if the JOIN statement Is longer than one line Copyright 2011, Information Builders. Slide 11 JOIN [LEFT_OUTER] field IN host [TAG tagname] TO [UNIQUE/MULTIPLE] field IN crfile [TAG tagname] [AS asname] [END]
JOIN Syntax and Techniques JOIN Syntax – DEFINE Based JOIN Copyright 2011, Information Builders. Slide 12 JOIN [LEFT_OUTER] define_field WITH real_field IN host TO [UNIQUE/MULTIPLE] field IN crfile [AS asname] [END] DEFINE FILE filename define_field/format=expression; END JOIN clears all DEFINEs unless SET KEEPDEFINES = ON
JOIN Syntax and Techniques JOIN Syntax – WHERE Based Copyright 2011, Information Builders. Slide 13 JOIN [LEFT_OUTER|INNER] hfld1 [AND hfld2...] IN hostfile [TAG tag1] TO [UNIQUE|MULTIPLE] crfield [AND crfld2...] IN crfile [TAG tag2] [AS joinname] WHERE expression; END JOIN LEFT_OUTER FILE EMPLOYEE AT DEPT_ID TAG T1 TO MULTIPLE FILE DEPARTMENT AT DEPT_ID TAG T2 AS J1. WHERE (T1.DEPT_ID EQ T2.DEPT_ID) AND (T2.DEPT_NAME EQ 'sales‘); END SELECT T1."emp_name",T1."dept_id",T2."dept_name",T2."dept_id" FROM ( dbo.emp T1 LEFT OUTER JOIN dbo.department T2 ON T2."dept_id" = T1."dept_id" AND (T2."dept_name" = 'sales') );
Examples Copyright 2011, Information Builders. Slide 14
JOIN Syntax and Techniques Examples Copyright 2011, Information Builders. Slide 15
JOIN Syntax and Techniques Examples JOIN COUNTRY IN WSUMCAR TO COUNTRY IN WSUMCARREG AS J1 Copyright 2011, Information Builders. Slide 16 TABLE FILE WSUMCAR SUM SALES BY REGION BY COUNTRY ON TABLE SET PAGE OFF END
JOIN Syntax and Techniques Examples JOIN LEFT_OUTER COUNTRY IN WSUMCAR TO COUNTRY IN WSUMCARREG AS J1 END Copyright 2011, Information Builders. Slide 17 TABLE FILE WSUMCAR SUM SALES BY REGION BY COUNTRY ON TABLE SET PAGE OFF END
JOIN Syntax and Techniques Examples -- Relational Datasource JOIN COUNTRY IN WSUMCARREG TO COUNTRY IN WSUMCAR AS J1 Copyright 2011, Information Builders. Slide 18 TABLE FILE WSUMCARREG PRINT COUNTRY WSUMCAR.COUNTRY CAR MODEL ON TABLE SET PAGE OFF END
JOIN Syntax and Techniques Examples -- Non-Relational Datasource JOIN COUNTRY IN WSUMCARREG TO COUNTRY IN WSUMCAR AS J1 Copyright 2011, Information Builders. Slide 19 TABLE FILE WSUMCARREG PRINT COUNTRY WSUMCAR.COUNTRY CAR MODEL ON TABLE SET PAGE OFF END
JOIN Syntax and Techniques Examples -- Non-Relational Datasource JOIN COUNTRY IN WSUMCARREG TO COUNTRY IN WSUMCAR AS J1 Copyright 2011, Information Builders. Slide 20 TABLE FILE WSUMCAR PRINT COUNTRY CAR MODEL WHERE COUNTRY EQ ‘ENGLAND’ ON TABLE SET PAGE OFF END
JOIN Syntax and Techniques Examples -- Non-Relational Datasource JOIN COUNTRY IN WSUMCARREG TO MULTIPLE COUNTRY IN WSUMCAR AS J1 END Copyright 2011, Information Builders. Slide 21 TABLE FILE WSUMCARREG PRINT COUNTRY WSUMCAR.COUNTRY CAR MODEL ON TABLE SET PAGE OFF END
Cluster Synonym Copyright 2011, Information Builders. Slide 22
JOIN Syntax and Techniques Cluster Synonym Copyright 2011, Information Builders. Slide 23 Synonym: FILENAME=WSUMCAR, SUFFIX=SQLORA, $ SEGMENT=SEG01, SEGTYPE=S0, $ FIELDNAME=COUNTRY, ALIAS=COUNTRY, USAGE=A10, ACTUAL=A10, $ FIELDNAME=CAR, ALIAS=CARS, USAGE=A16, ACTUAL=A16, $ FIELDNAME=MODEL, ALIAS=MODEL, USAGE=A24, ACTUAL=A24, $ FIELDNAME=DEALER_COST, ALIAS=DCOST, USAGE=D7, ACTUAL=D8, $ FIELDNAME=RETAIL_COST, ALIAS=RCOST, USAGE=D7, ACTUAL=D8, $ FIELDNAME=SALES, ALIAS=UNITS, USAGE=I6, ACTUAL=I4, $ Access: SEGNAME=SEG01, TABLENAME=WSUMCAR, KEYS=03, WRITE=YES, $
JOIN Syntax and Techniques Cluster Synonym Copyright 2011, Information Builders. Slide 24 Synonym: FILENAME=WSUMCARREG, SUFFIX=SQLORA, $ SEGMENT=SEG01, SEGTYPE=S0, $ FIELDNAME=COUNTRY, ALIAS=COUNTRY, USAGE=A10, ACTUAL=A10, $ FIELDNAME=REGION, ALIAS=REGION, USAGE=A10, ACTUAL=A10, MISSING=ON, $ Access: SEGNAME=SEG01, TABLENAME=WSUMCARREG, KEYS=01, WRITE=YES, $
Cluster Synonym – Using the Tool Copyright 2011, Information Builders. Slide 25
JOIN Syntax and Techniques Cluster Synonym – Synonym Editor Open The Root Synonym Copyright 2011, Information Builders. Slide 26
JOIN Syntax and Techniques Cluster Synonym – Synonym Editor Open The Root Synonym Copyright 2011, Information Builders. Slide 27 Click on Modeling View Tab
JOIN Syntax and Techniques Cluster Synonym – Synonym Editor Copyright 2011, Information Builders. Slide 28 Click on Insert Reference to Segment from Existing Synonym
JOIN Syntax and Techniques Cluster Synonym – Synonym Editor Copyright 2011, Information Builders. Slide 29 Select The Table/File To Add.
JOIN Syntax and Techniques Cluster Synonym – Synonym Editor Copyright 2011, Information Builders. Slide 30 Default JOIN Type Is INNER To Multiple
JOIN Syntax and Techniques Cluster Synonym – Synonym Editor Copyright 2011, Information Builders. Slide 31 In Our Case, Select Left Outer Join
JOIN Syntax and Techniques Cluster Synonym – Synonym Editor Copyright 2011, Information Builders. Slide 32 To View The Code, Click On Text View and Access File Text View tabs
JOIN Syntax and Techniques Cluster Synonym Copyright 2011, Information Builders. Slide 33 Synonym: FILENAME=wsumcarc, SUFFIX=SQLORA, $ SEGMENT=SEG01, SEGTYPE=S0, $ FIELDNAME=COUNTRY, ALIAS=COUNTRY, USAGE=A10, ACTUAL=A10, $ FIELDNAME=CAR, ALIAS=CARS, USAGE=A16, ACTUAL=A16, $ FIELDNAME=MODEL, ALIAS=MODEL, USAGE=A24, ACTUAL=A24, $ FIELDNAME=DEALER_COST, ALIAS=DCOST, USAGE=D7, ACTUAL=D8, $ FIELDNAME=RETAIL_COST, ALIAS=RCOST, USAGE=D7, ACTUAL=D8, $ FIELDNAME=SALES, ALIAS=UNITS, USAGE=I6, ACTUAL=I4, $ SEGMENT=SEG011, SEGTYPE=KM, PARENT=SEG01, CRFILE=REPRO_NFS/WSUMCARREG, CRSEGMENT=SEG01, CRINCLUDE=ALL, CRJOINTYPE=LEFT_OUTER, JOIN_WHERE=SEG01.COUNTRY EQ SEG011.COUNTRY;, $ Access: SEGNAME=SEG01, TABLENAME=WSUMCAR, KEYS=03, WRITE=YES, $ SEGNAME=SEG011, $
JOIN Syntax and Techniques Cluster Synonym Copyright 2011, Information Builders. Slide 34 SEGMENT=SEG011, SEGTYPE=KM, PARENT=SEG01, CRFILE=REPRO_NFS/WSUMCARREG, CRSEGMENT=SEG01, CRINCLUDE=ALL, CRJOINTYPE=LEFT_OUTER, JOIN_WHERE=SEG01.COUNTRY EQ SEG011.COUNTRY;, $ SEGTYPE=KM (KU) - Keyed Multiple/Keyed Unique PARENT=SEG01- Identifies Host CRFILE=REPRO_NFS/WSUMCARREG- Name Of Synonym CRSEGMENT=SEG01- Segment To Use In Synonym CRJOINTYPE=LEFT_OUTER (INNER)- Relationship JOIN_WHERE=JOIN predicate expression CRINCLUDE=ALL - Use FIELDNAMES from the Cross reference synonym
Considerations Copyright 2011, Information Builders. Slide 35
JOIN Syntax and Techniques Considerations Traces SET TRACEOFF=ALL SET TRACEON=SQLAGGR//CLIENT SET TRACEON=STMTRACE//CLIENT SET TRACEON=SQLCALL//CLIENT SET TRACESTAMP=OFF SET TRACEUSER=ON Copyright 2011, Information Builders. Slide 36
JOIN Syntax and Techniques Considerations Multiplicative Effect Parent Data Will Be Generated For Each Child Record Table A – A 1, B 2, C3 Table B – A 5, A 7, A 2, B 0, B1, C2 Joining A to B Will Produce: A 1 5 A 1 7 A 1 2 If You Subtotaled The Values: 3 14 Even Though A 1 Only Occurred Once In Table A Copyright 2011, Information Builders. Slide 37
JOIN Syntax and Techniques Overview When To Combine Files Type Of JOINs JOIN Techniques JOIN Syntax Joining Files In The Synonym Considerations Review Copyright 2011, Information Builders. Slide 38
Questions Copyright 2011, Information Builders. Slide 39
Thank You For Attending Copyright 2011, Information Builders. Slide 40