joinkeys in sort jcl examples

Mastering Joinkeys in Sort JCL: Expert Examples & Practical Tips

By Laura Martin
AD

In the world of Mainframe computers, sort is a crucial function that arranges and organizes data in a specified manner. JCL (Job Control Language) is used to instruct and manage these processes. Joinkeys play a pivotal role in sorting when dealing with multi-files. They enable precise sorting (%) and merge of files based on matching fields. Let's delve into examples of JOINKEYS in sort JCL.

Sorting can be complex, especially when dealing with multi-files and duplicate data. Joinkeys, like JOINFIELDS but with a different function, ensure a precise sort and merge of input files.

Understanding JOINKEYS in Sort JCL

JOINKEYS define the join field for the sort and merge processes. They decide how records from multiple files are grouped together during sorting.

AD
a poster with the names and symbols for different types of letters
a poster with the names and symbols for different types of letters
a blue and white poster with the names of different types of letters on it's side
a blue and white poster with the names of different types of letters on it's side
My SQL join types
My SQL join types

JOINKEYS can be categorize into different groups depending on the type of sorting. They are used in conjunction with the SORT keyword in JCL.

Single Level Joinkeys (% lemma)

A joinkey of the form (field_name) implies that records from multiple input files should be grouped together if the field named matches across files. This is also known as a % lemma join.

What are Semi, Anti, and Natural Joins in SQL?
What are Semi, Anti, and Natural Joins in SQL?
SQL Joins Explained with Examples | Beginner SQL Guide
SQL Joins Explained with Examples | Beginner SQL Guide
SQL JOINS Explained for Behinners
SQL JOINS Explained for Behinners

For example, JOINKEYS=(EMPNO.EMPNAME) sorts and merges records where the employee number (EMPNO) and name (EMPNAME) match, producing a single output file.

Multiple Level Joinkeys (% lemma nested)

More complex joinkeys, like (field_name1.field_name2), sort and merge records at multiple levels. This is known as a nested % lemma join.

AD

A JOINKEY of JOINKEYS=(DEPTNO.DIVISION) first groups records by department number (DEPTNO), then within each group, by division (DIVISION).

SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
an image of a diagram with the names and symbols for soljoins
an image of a diagram with the names and symbols for soljoins
SQL Joins Explained Better Than Most Tutorials
SQL Joins Explained Better Than Most Tutorials
a poster with the words, 20 essential skills for students to learn
a poster with the words, 20 essential skills for students to learn
Types of Joins in SQL Explained Clearly 📊 | Inner Join, Left Join, Right Join, Full Join |
Types of Joins in SQL Explained Clearly 📊 | Inner Join, Left Join, Right Join, Full Join |
a computer screen with some text on it
a computer screen with some text on it

Practical Examples of JOINKEYS

Let's explore some practical JCL examples using JOINKEYS.

Assuming we have two input files (INFILE1 and INFILE2) with similar structures, and we want to sort and merge them based on the 'KEYFIELD' field.

AD

Single File Sort using JOINKEYS

For a single-level sort, we can use the following JCL:

the four circles are labeled in blue and have different names on each one, including left joinr, right joinr, center joinr,
the four circles are labeled in blue and have different names on each one, including left joinr, right joinr, center joinr,
DBMS JOIN Operations Explained | SQL INNER JOIN, LEFT JOIN, RIGHT JOIN & FULL JOIN Guide
DBMS JOIN Operations Explained | SQL INNER JOIN, LEFT JOIN, RIGHT JOIN & FULL JOIN Guide
TruthBytes - #SQL Joins | Facebook
TruthBytes - #SQL Joins | Facebook
SQL Joins Explained 🚀 | Beginner Data Analyst SQL Guide
SQL Joins Explained 🚀 | Beginner Data Analyst SQL Guide
an info sheet showing the different types of font and numbers for each type of text
an info sheet showing the different types of font and numbers for each type of text
two vennuous circles with numbers in them and the same number on each one
two vennuous circles with numbers in them and the same number on each one
two circles with different types of tables and numbers on each one, labeled table 1
two circles with different types of tables and numbers on each one, labeled table 1
the complete guide to mastering johns in sol
the complete guide to mastering johns in sol
four circles with the names and numbers in each circle, labeled to be able to label them
four circles with the names and numbers in each circle, labeled to be able to label them

SORT WORK=( ...,JOINKEYS=(KEYFIELD,LPLFIELD),... )

This sorts the work files by the KEYFIELD, and if there are any record layout discrepancies, by LPLFIELD (_last_page_field).

AD

Multiple Files Sort using JOINKEYS

To sort and merge multiple files, we add the MERGE parameter to the SORT step:

SORT WORK=( ...,JOINKEYS=(KEYFIELD,LPLFIELD),MERGE,... )

After the sort, records with matching KEYFIELDS from both input files are merged into a single, sorted output file (WORK).

In conclusion, JOINKEYS in sort JCL are powerful tools for sorting and merging multi-files. Understanding their usage and deploying them effectively can significantly enhance data management in Mainframe environments. Learning to leverage JOINKEYS can improve your productivity and program efficiency. Happy coding!"

AD