Login
Register
Search
Home
Forums
Jobs
LawsonGuru
LawsonGuru Letter
LawsonGuru Blog
Worthwhile Reading
Infor Lawson News Feed
Store
Store FAQs
About
Forums
Human Capital Management
Lawson S3 HR/Payroll/Benefits
Combining multiple records into one using SQL
Home
Forums
Jobs
LawsonGuru
LawsonGuru Letter
LawsonGuru Blog
Worthwhile Reading
Infor Lawson News Feed
Store
Store FAQs
About
Who's On?
Membership:
Latest:
John Bonin
Past 24 Hours:
0
Prev. 24 Hours:
0
Overall:
5270
People Online:
Visitors:
139
Members:
0
Total:
139
Online Now:
New Topics
Lawson S3 Procurement
Tolerance Settings
3/31/2025 2:01 PM
I've been trying to set a tolerance for some t
Dealing with Lawson / Infor
Printing Solutions other than MHC
3/27/2025 1:00 PM
What are others using for printing solutions besid
Lawson S3 Procurement
Green check marks in Lawson 9.0.1
3/20/2025 4:55 PM
Hi, How to remove green check mark on items when o
Lawson S3 HR/Payroll/Benefits
Pay Rate History to Show All Positions
2/26/2025 3:34 PM
Does anyone know how to modify payratehistory.htm
Infor CloudSuite
How to build a Pre-Prod tenant
2/7/2025 1:28 AM
After we finished our implementation and ended our
Lawson S3 Procurement
Browser issue with RQC Shopping
1/28/2025 5:49 PM
Since the recent Chrome/Edge updates, our RQC shop
Lawson S3 Procurement
S3-Procurement New Company
1/22/2025 10:38 PM
My Accounting Department has created a new Company
S3 Customization/Development
JUSTIFIED RIGHT
1/15/2025 7:41 PM
Is there a way in Lawson COBOL to make a character
S3 Systems Administration
ADFS certificate - new cert
12/3/2024 9:38 PM
The certificates on the windows boxes expired and
Lawson S3 HR/Payroll/Benefits
Post Tax Benefit Plan Table
11/14/2024 9:16 PM
Hi, totally new to Laswon. I have a repor
Top Forum Posters
Name
Points
Greg Moeller
4184
David Williams
3349
JonA
3291
Kat V
2984
Woozy
1973
Jimmy Chiu
1883
Kwane McNeal
1437
Ragu Raghavan
1375
Roger French
1315
mark.cook
1244
Forums
Filtered Topics
Unanswered
Unresolved
Announcements
Active Topics
Most Liked
Most Replies
Search Forums
Search
Advanced Search
Topics
Posts
Prev
Next
Forums
Human Capital Management
Lawson S3 HR/Payroll/Benefits
Combining multiple records into one using SQL
Please
login
to post a reply.
4 Replies
1
Subscribed to this topic
68 Subscribed to this forum
Sort:
Oldest First
Most Recent First
Author
Messages
Leonard Courchaine
New Member
Posts: 0
4/14/2010 11:44 AM
We're trying to write a SQL statement that will take multiple rows of data (in this case, emergency contact data from the PAEMERGCNT table) and combine that into 1 record per employee. As you may know, in that table, there can be multiple records (many emergency contacts) for each employee. For an interface we're doing, we need to get all the emergency contacts into one record with the fields like contact1fname, contact1lname, contact2fname, contact2lname, etc.,.
Any ideas on how to do that using SQL?
Thanks very much,
Lenny
Miki M
New Member
Posts: 2
4/14/2010 12:45 PM
Split
This is an example of how I did it for the PAEMPPOS info. You could do the same thing with the contact table using the SEQ_NBR instead of the pos_level. We have a maximum of 5 positions per employee so I've done 5 sections. You would need to determine your maximum number of emergency contacts. I'm sure there is a sharper way to do it, my SQL skills are limited! Hope this helps.
-- Pull all active positions by employee into one row.
select
employee
--Pos 1
,max(case when pos_level = '1' then POSITION end) Pos1
,max(case when pos_level = '1' then POS_LEVEL end) PosLvl1
,max(case when pos_level = '1' then FTE end) FTE1
,max(case when pos_level = '1' then EFFECT_DATE end) EffDt1
,max(case when pos_level = '1' then POS_EFF_DT end) PosEffDt1
,max(case when pos_level = '1' then JOB_CODE end) Job1
,max(case when pos_level = '1' then PROCESS_LEVEL end) PL1
,max(case when pos_level = '1' then DEPARTMENT end) Dept1
,max(case when pos_level = '1' then EXP_ACCT_UNIT end) AcctUnit1
,max(case when pos_level = '1' then SCHEDULE end) Sched1
,max(case when pos_level = '1' then LOCAT_CODE end) Loc1
,max(case when pos_level = '1' then WORK_SCHED end) Work_Sched1
,max(case when pos_level = '1' then USER_LEVEL end) UL1
,max(case when pos_level = '1' then SUPERVISOR end) Supv1
,max(case when pos_level = '1' then SUPERVISOR_IND end) Supv_Ind1
,max(case when pos_level = '1' then PAY_RATE end) Rate1
,max(case when pos_level = '1' then PAY_GRADE end) Grade1
,max(case when pos_level = '1' then PAY_STEP end) Step1
,max(case when pos_level = '1' then ANNUAL_HOURS end) Hours1
--Pos2
,max(case when pos_level = '2' then POSITION end) Pos2
,max(case when pos_level = '2' then POS_LEVEL end) PosLvl2
,max(case when pos_level = '2' then FTE end) FTE2
,max(case when pos_level = '2' then EFFECT_DATE end) EffDt2
,max(case when pos_level = '2' then POS_EFF_DT end) PosEffDt2
,max(case when pos_level = '2' then JOB_CODE end) Job2
,max(case when pos_level = '2' then PROCESS_LEVEL end) PL2
,max(case when pos_level = '2' then DEPARTMENT end) Dept2
,max(case when pos_level = '2' then EXP_ACCT_UNIT end) AcctUnit2
,max(case when pos_level = '2' then SCHEDULE end) Sched2
,max(case when pos_level = '2' then LOCAT_CODE end) Loc2
,max(case when pos_level = '2' then WORK_SCHED end) Work_Sched2
,max(case when pos_level = '2' then USER_LEVEL end) UL2
,max(case when pos_level = '2' then SUPERVISOR end) Supv2
,max(case when pos_level = '2' then SUPERVISOR_IND end) Supv_Ind2
,max(case when pos_level = '2' then PAY_RATE end) Rate2
,max(case when pos_level = '2' then PAY_GRADE end) Grade2
,max(case when pos_level = '2' then PAY_STEP end) Step2
,max(case when pos_level = '2' then ANNUAL_HOURS end) Hours2
----Pos3
,max(case when pos_level = '3' then POSITION end) Pos3
,max(case when pos_level = '3' then POS_LEVEL end) PosLvl3
,max(case when pos_level = '3' then FTE end) FTE3
,max(case when pos_level = '3' then EFFECT_DATE end) EffDt3
,max(case when pos_level = '3' then POS_EFF_DT end) PosEffDt3
,max(case when pos_level = '3' then JOB_CODE end) Job3
,max(case when pos_level = '3' then PROCESS_LEVEL end) PL3
,max(case when pos_level = '3' then DEPARTMENT end) Dept3
,max(case when pos_level = '3' then EXP_ACCT_UNIT end) AcctUnit3
,max(case when pos_level = '3' then SCHEDULE end) Sched3
,max(case when pos_level = '3' then LOCAT_CODE end) Loc3
,max(case when pos_level = '3' then WORK_SCHED end) Work_Sched3
,max(case when pos_level = '3' then USER_LEVEL end) UL3
,max(case when pos_level = '3' then SUPERVISOR end) Supv3
,max(case when pos_level = '3' then SUPERVISOR_IND end) Supv_Ind3
,max(case when pos_level = '3' then PAY_RATE end) Rate3
,max(case when pos_level = '3' then PAY_GRADE end) Grade3
,max(case when pos_level = '3' then PAY_STEP end) Step3
,max(case when pos_level = '3' then ANNUAL_HOURS end) Hours3
--Pos4
,max(case when pos_level = '4' then POSITION end) Pos4
,max(case when pos_level = '4' then POS_LEVEL end) PosLvl4
,max(case when pos_level = '4' then FTE end) FTE4
,max(case when pos_level = '4' then EFFECT_DATE end) EffDt4
,max(case when pos_level = '4' then POS_EFF_DT end) PosEffDt4
,max(case when pos_level = '4' then JOB_CODE end) Job4
,max(case when pos_level = '4' then PROCESS_LEVEL end) PL4
,max(case when pos_level = '4' then DEPARTMENT end) Dept4
,max(case when pos_level = '4' then EXP_ACCT_UNIT end) AcctUnit4
,max(case when pos_level = '4' then SCHEDULE end) Sched4
,max(case when pos_level = '4' then LOCAT_CODE end) Loc4
,max(case when pos_level = '4' then WORK_SCHED end) Work_Sched4
,max(case when pos_level = '4' then USER_LEVEL end) UL4
,max(case when pos_level = '4' then SUPERVISOR end) Supv4
,max(case when pos_level = '4' then SUPERVISOR_IND end) Supv_Ind4
,max(case when pos_level = '4' then PAY_RATE end) Rate4
,max(case when pos_level = '4' then PAY_GRADE end) Grade4
,max(case when pos_level = '4' then PAY_STEP end) Step4
,max(case when pos_level = '4' then ANNUAL_HOURS end) Hours4
--Pos5
,max(case when pos_level = '5' then POSITION end) Pos5
,max(case when pos_level = '5' then POS_LEVEL end) PosLvl5
,max(case when pos_level = '5' then FTE end) FTE5
,max(case when pos_level = '5' then EFFECT_DATE end) EffDt5
,max(case when pos_level = '5' then POS_EFF_DT end) PosEffDt5
,max(case when pos_level = '5' then JOB_CODE end) Job5
,max(case when pos_level = '5' then PROCESS_LEVEL end) PL5
,max(case when pos_level = '5' then DEPARTMENT end) Dept5
,max(case when pos_level = '5' then EXP_ACCT_UNIT end) AcctUnit5
,max(case when pos_level = '5' then SCHEDULE end) Sched5
,max(case when pos_level = '5' then LOCAT_CODE end) Loc5
,max(case when pos_level = '5' then WORK_SCHED end) Work_Sched5
,max(case when pos_level = '5' then USER_LEVEL end) UL5
,max(case when pos_level = '5' then SUPERVISOR end) Supv5
,max(case when pos_level = '5' then SUPERVISOR_IND end) Supv_Ind5
,max(case when pos_level = '5' then PAY_RATE end) Rate5
,max(case when pos_level = '5' then PAY_GRADE end) Grade5
,max(case when pos_level = '5' then PAY_STEP end) Step5
,max(case when pos_level = '5' then ANNUAL_HOURS end) Hours5
from paemppos PA where
end_date = TO_DATE ('01-JAN-1700', 'DD-MON-YYYY')
group by
employee
Leonard Courchaine
New Member
Posts: 0
4/14/2010 1:01 PM
Split
Thanks very much, Miki. My SQL skills are also limited. I didn't want to make my original note too long so I left out the fact that I've used your method but, unfortunately, because of the way the seq_nbr is utilized in paemergcnt, it's ugly. It looks like the seq_nbr keeps incrementing so that I have one employee, for example, whose records use up seq_nbr 4, 6, 8 and 9 (no doubt due to changes/additions/deletions of contacts over time). So, I have this going up to 10 contacts so these are covered. It ends up being long and ugly so that what I end up sending to the other system is 10 instances of emergency contacts with, for most, only 1 used. I was thinking there must be a better way. Thanks again for your input.
Deleted User
New Member
Posts: 0
4/14/2010 1:42 PM
Split
Here is an idea on how to do it. I have it do up to three but you may take it as far as you want.
SELECT LTRIM(RTRIM(c1.FIRST_NAME)) + ' ' + LTRIM(RTRIM(c1.LAST_NAME)) AS FULL_NAME1
, c1.HM_PHONE_NBR
, c1.WK_PHONE_NBR
,LTRIM(RTRIM(c2.FIRST_NAME)) + ' ' + LTRIM(RTRIM(c2.LAST_NAME)) AS FULL_NAME2
, c2.HM_PHONE_NBR
, c2.WK_PHONE_NBR
,LTRIM(RTRIM(c3.FIRST_NAME)) + ' ' + LTRIM(RTRIM(c3.LAST_NAME)) AS FULL_NAME3
, c3.HM_PHONE_NBR
, c3.WK_PHONE_NBR
FROM PAEMERGCNT c1
LEFT OUTER JOIN PAEMERGCNT c2
ON c1.COMPANY = c2.COMPANY
AND c1.EMPLOYEE = c2.EMPLOYEE
AND c1.SEQ_NBR <> c2.SEQ_NBR
AND c2.SEQ_NBR =
(
SELECT MIN(SEQ_NBR)
FROM PAEMERGCNT c_next
WHERE c_next.COMPANY = c2.COMPANY
AND c_next.EMPLOYEE = c2.EMPLOYEE
AND c_next.SEQ_NBR <> c1.SEQ_NBR
)
LEFT OUTER JOIN PAEMERGCNT c3
ON c1.COMPANY = c3.COMPANY
AND c1.EMPLOYEE = c3.EMPLOYEE
AND c1.SEQ_NBR <> c2.SEQ_NBR
AND c2.SEQ_NBR <> c3.SEQ_NBR
AND c3.SEQ_NBR =
(
SELECT MIN(SEQ_NBR)
FROM PAEMERGCNT c_next
WHERE c_next.COMPANY = c2.COMPANY
AND c_next.EMPLOYEE = c2.EMPLOYEE
AND c_next.SEQ_NBR <> c1.SEQ_NBR
AND c_next.SEQ_NBR <> c2.SEQ_NBR
)
WHERE c1.SEQ_NBR =
(
SELECT MIN(SEQ_NBR)
FROM PAEMERGCNT c_min
WHERE c_min.COMPANY = c1.COMPANY
AND c_min.EMPLOYEE = c1.EMPLOYEE
)
John Henley
Posts: 3351
4/14/2010 2:01 PM
Split
Depending on your backend db, there are various ways in SQL to create this using pivots. =
Please
login
to post a reply.