Solved

SQL Syntax across multiple fields

Posted on 2014-12-17
2
91 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

743 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

14 Experts available now in Live!

Get 1:1 Help Now