Solved

SQL Syntax across multiple fields

Posted on 2014-12-17
2
93 Views
Last Modified: 2014-12-19
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
Comment
Question by:rwheeler23
2 Comments
 
LVL 35

Accepted Solution

by:
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'

Open in new window

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

Open in new window

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

by:rwheeler23
ID: 40510367
Excellent solution. Thank you.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now