Merge two files using Sort in batch - jcl

So i have two files that i need to merge. File A contains the key. Im unsure how to do this using SORT in batch (JCL). I know I need to use joinkey or ifthen. Would anyone know a solution to this?
Any help is greatly appreciated.
File A:
000001EMPLOYEE ID # 1
000002EMPLOYEE ID # 2
000003EMPLOYEE ID # 3
000004EMPLOYEE ID # 4
000005EMPLOYEE ID # 5
000006EMPLOYEE ID # 6
000007EMPLOYEE ID # 7
000008EMPLOYEE ID # 8
000009EMPLOYEE ID # 9
000010EMPLOYEE ID # 10
File B:
000001 John Doe
000002 Sam Maguire
000003 Jane Doe
000006 Jackson
000007 James Bond
000008 Spiderman
000019 Not an Employee
Desired output:
000001 EMPLOYEE ID # 1 John Doe
000002 EMPLOYEE ID # 2 Sam Maguire
000003 EMPLOYEE ID # 3 Jane Doe
000004 EMPLOYEE ID # 4
000005 EMPLOYEE ID # 5
000006 EMPLOYEE ID # 6 Jackson
000007 EMPLOYEE ID # 7 James Bond
000008 EMPLOYEE ID # 8 Spiderman
000009 EMPLOYEE ID # 9
000010 EMPLOYEE ID # 10
000019 Not an Employee

To join records in two files on common fields, you can use the DFSORT JoinKeys command.
Input file: empData.txt
000001EMPLOYEE ID # 1
000002EMPLOYEE ID # 2
000003EMPLOYEE ID # 3
000004EMPLOYEE ID # 4
000005EMPLOYEE ID # 5
000006EMPLOYEE ID # 6
000007EMPLOYEE ID # 7
000008EMPLOYEE ID # 8
000009EMPLOYEE ID # 9
000010EMPLOYEE ID # 10
Input file: empNames.txt
000001 John Doe
000002 Sam Maguire
000003 Jane Doe
000006 Jackson
000007 James Bond
000008 Spiderman
000019 Not an Employee
Input file: control.txt
* Employee Number in 1-6-EmpData.txt
JOINKEYS FILE=F1,FIELDS=(1,6,A)
* Employee Number in 1-6-EmpNames.txt
JOINKEYS FILE=F2,FIELDS=(1,6,A)
* Copy Name to EmpData
* Put file indicator (?) in column1
* This will be either 1,2 or B
REFORMAT FIELDS=(?,F1:1,26,F2:1,23)
JOIN UNPAIRED,F1,F2
* Use Change to see if record was only in file 2
* and replace employee number from file2 in output
OUTREC FIELDS=(1,1,CHANGE=(6,
C'2',28,6),NOMATCH=(2,6),
X,8,19,35,15)
END
Output File: joined.txt
000001 EMPLOYEE ID # 1 John Doe
000002 EMPLOYEE ID # 2 Sam Maguire
000003 EMPLOYEE ID # 3 Jane Doe
000004 EMPLOYEE ID # 4
000005 EMPLOYEE ID # 5
000006 EMPLOYEE ID # 6 Jackson
000007 EMPLOYEE ID # 7 James Bond
000008 EMPLOYEE ID # 8 Spiderman
000009 EMPLOYEE ID # 9
000010 EMPLOYEE ID # 10
000019 Not an Employee
The REFORMAT FIELDS ? places a '1','2' or 'B' in the output record to indicate how the joined record was built. If it is '2', then the record only occurred in the second file so we use the CHANGE function to get the Employee Id from the second file and place it in the output record.
Command Line:
ahlsort control.txt "empData.txt,dcb=(recfm=T,lrecl=100),empNames.txt,dcb=(recfm=T,lrecl=100)" "joined.txt,dcb=(recfm=T,lrecl=200)"
This was tested with AHLSORT v14r3-227 for Windows but should work the same on AHLSORT for Linux or DFSORT on the mainframe.

Related

DISTINCT ON with a One to Many Relation through another table

I have four Postgres tables:
Professions
id
name
123
profession 1
345
profession 2
Versions
id
professionId
status
createdAt
567
123
Live
2020-01-01
999
123
Draft
2021-02-01
666
345
Live
2021-01-01
Organisations
id
name
333
organisation 1
444
organisation 2
655
organisation 3
ProfessionToOrganisation
id
professionId
organisationId
Role
665
123
333
Regulator
533
123
444
Something else
534
345
444
Regulator
I am trying to get the latest version of each profession, regardless of their status, so am using a combination of DISTINCT ON and ORDER BY like so:
SELECT DISTINCT ON (
professionVersions.professionId,
professions.name
) professionVersions.id AS "professionVersions_id", professions.name, organisations.name
FROM professionVersions
LEFT JOIN professions ON professions.id = professionVersions.professionId
LEFT JOIN professionToOrganisation ON
professionToOrganisation.professionId = professions.id
LEFT JOIN organisations ON professionToOrganisation.organisationId = organisations.id
ORDER BY
professions.name,
professionVersions.professionId,
professionVersions.created_at DESC
However, I only get one organisation returned for each profession, i.e:
professionVersions_id
name
name
999
Profession 1
Organisation 1
666
Profession 2
Organisation 2
Whereas I wouild like:
professionVersions_id
name
name
999
Profession 1
Organisation 1
999
Profession 1
Organisation 2
666
Profession 2
Organisation 2
How do I go about achieving this?
I've got a DB Fiddle here - https://www.db-fiddle.com/f/44iWbw7oyDtVMsQfE45jPA/0

How to hidden value when it doesn't match

I'm in a trouble like this:
For a following table A with data
|ID| |Department| |Name|
1 IT JOHN
3 IT JOHN
5 IT JOHN
1 SALE LINH
3 SALE LINH
2 SALE LINH
3 CLERK MINA
6 CLERK MINA
4 CLERK MINA
There are some ROLEID, each ROLEID has role corresponding, such as 1 is Manager,2 is leader...
I want to select based on ROLEID, if exist ROLEID = 3, all values has department is IT won't display. How can I do that?

query multiple attribute in a table with single attribute in another table

I can't explain my problem in English well. So I write my problem in a personal way.
user_id name surname
1 john great
2 mary white
3 joseph alann
event_id official_id assistant_id date
1 1 2 2017-12-19
2 1 3 2017-12-20
3 2 3 2017-12-21
I want to get names at the same time when I query an event. I tried:
SELECT * FROM event a, user b WHERE a.official_id=b.user_id AND a.assistant_id=b.user_id
When I use "OR" instead of "AND" gives me cartesian result. I want the result like:
event_id off_id off_name asst_id asst_name date
1 1 john 2 mary 2017-12-19
2 1 john 3 joseph 2017-12-20
3 2 mary 3 joseph 2017-12-21

PostgreSQL Query to get the output from same table with same table values

I have one table
id employee leave_days leave_type type
1 ABC 10 sick remove
2 ABC 20 sick add
3 ABC 15 Annual remove
4 ABC 50 Annual add
5 XYZ 10 sick remove
6 XYZ 20 sick add
7 XYZ 15 Annual remove
8 XYZ 50 Annual add
From the above table I will group by the column name called leave_type and then I will merge rows and the output should be as follows.
I have to group by column name leave_type and add new column called leave_allocated . In the leave_allocated column, the column type with value add only will come.
id employee leave_days leave_type leave_allocated
1 ABC 10 sick 20
2 ABC 15 Annual 50
3 XYZ 10 sick 20
4 XYZ 15 Annual 50
I tried with sub query I could not match the inner query with outer query .
This should help
SELECT id,
employee,
leave_dates,
leave type,
(SELECT leave_days
FROM TABLE t2
WHERE t2.id = t1.id
AND t2.type = 'add'
) leave_allocated
FROM TABLE t1
WHERE t1.type = 'remove'

Return all records regardless if there is a match

In my Table 1, It may have AND have a null entry in the address column to corresponding record OR not have a matching entry in Table 2.
I want to present all the records in Table 1 but also present corresponding entries from Table 2. My RESULT is what I am trying to achieve.
Table 1
ID First Last
1 John Smith
2 Bob Long
3 Bill Davis
4 Sam Bird
5 Tom Fenton
6 Mary Willis
Table 2
RefID ID Address
1 1 123 Main
2 2 555 Center
3 3 626 Smith
4 4 412 Walnut
5 1
6 2 555 Center
7 3
8 4 412 Walnut
Result
Id First Last Address
1 John Smith 123 Main
2 Bob Long 555 Center
3 Bill Davis 626 Smith
4 Sam Bird 412 Walnut
5 Tom Fenton
6 Mary Willis
You need an outer join for this:
SELECT * FROM Table1 t1 LEFT OUTER JOIN Table2 t2 ON t1.ID = t2.RefID
How do you join those two tables? If table 2 have more than 1 matched address, how do you want display them? Please clarify in your question.
Here is a query based on my assumptions.
SELECT
ID, First, Last,
Address = (SELECT MAX(Address) FROM Table2 t2 WHERE t1.ID = t2.ID)
FROM Table1 t1