Solved

# SQL Syntax across multiple fields

Posted on 2014-12-17
98 Views
I have a payroll puzzle to solve. There are two tables involved in this puzzle. One has the payroll detail information and the second has the potential accounts that correspond to those records.  In the sample dataset there are two employees(1000 and 1200). For each payroll record I need to obtain the ACCOUNT field value from the PAYACCTS table. The trick is the PAYACCTS table because there are 'Catch All' records. If no exact values match then the value on the catch all record is used. So the answers for each record are:

1-6000 because 025 matches 025,TEC matches TEC and HOURLY matches HOURLY
2-5000 because 030 matches nothing so it falls to ALL,TEC falls to ALL and HOURLY matches HOURLY
3-7 all go to 5000 for the same reason as 2
8-7000 because DEPTMENT,POSITION and PAYCODE all do not match. The 'Catch All' gets this one.

Essentially the comparison proceed from left to right. DEPTMENT first, then POSITION next and lastly PAYCODE.

Can this be with a single SQL script or should I make a function or stored procedure for this?
SAMPLEPAYROLL.txt
0
Question by:rwheeler23
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 35

Accepted Solution

Robert Schutt earned 500 total points
ID: 40506236
You could make a LEFT JOIN for each type of match you want to allow and use COALESCE to choose the order of preference, the first non-null field will be chosen:
``````select PD.*, PA1.ACCOUNT, PA2.ACCOUNT, PA3.ACCOUNT, PA4.ACCOUNT, COALESCE(PA1.ACCOUNT, PA2.ACCOUNT, PA3.ACCOUNT, PA4.ACCOUNT) ACCOUNT from PAYDETAIL PD
LEFT JOIN PAYACCTS PA1 ON PA1.DEPTMENT = PD.DEPTMENT AND PA1.POSITION = PD.POSITION AND PA1.PAYCODE = PD.PAYCODE
LEFT JOIN PAYACCTS PA2 ON PA2.DEPTMENT = 'ALL' AND PA2.POSITION = PD.POSITION AND PA2.PAYCODE = PD.PAYCODE
LEFT JOIN PAYACCTS PA3 ON PA3.DEPTMENT = 'ALL' AND PA3.POSITION = 'ALL' AND PA3.PAYCODE = PD.PAYCODE
LEFT JOIN PAYACCTS PA4 ON PA4.DEPTMENT = 'ALL' AND PA4.POSITION = 'ALL' AND PA4.PAYCODE = 'ALL'
``````
output:
``````EMPLOYID	DATE		DEPTMENT	POSITION	PAYCODE	AMOUNT	HOURS	ACCOUNT	ACCOUNT	ACCOUNT	ACCOUNT	ACCOUNT
1000		2014-10-21	025		TEC		HOURLY	100.00	2.00	6000	NULL	5000	7000	6000
1000		2014-10-21	030		TEC		HOURLY	200.00	4.00	NULL	NULL	5000	7000	5000
1000		2014-10-21	035		TEC		HOURLY	125.00	8.00	NULL	NULL	5000	7000	5000
1000		2014-10-21	040		TEC		HOURLY	150.00	16.00	NULL	NULL	5000	7000	5000
1000		2014-10-21	045		TEC		HOURLY	175.00	24.00	NULL	NULL	5000	7000	5000
1000		2014-10-21	050		TEC		HOURLY	100.00	32.00	NULL	NULL	5000	7000	5000
1000		2014-10-21	055		TEC		HOURLY	150.00	40.00	NULL	NULL	5000	7000	5000
1200		2014-10-21	055		ABS		SALARY	150.00	40.00	NULL	NULL	NULL	7000	7000
``````
I included the individual PAx columns in the output but you would only need the last one in the actual query.

It could be expanded for other situations like including fallback for PAYCODE where only DEPTMENT and POSITION match, but that's not how I read your question.

If this is faster using a stored procedure depends to a degree on your data (structure and amount) and how you query it (like monthy, yearly). In general, you can't beat a (relatively) simple query with a loop in a procedure. The problem is determining if it is a simple query, you would need to look at the execution plan in your environment for that. Make sure there is an index on PAYACCTS(DEPTMENT,POSITION,PAYCODE)
0

Author Closing Comment

ID: 40510367
Excellent solution. Thank you.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
###### Suggested Courses
Course of the Month10 days, 16 hours left to enroll