Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 102
  • Last Modified:

SQL Syntax across multiple fields

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
rwheeler23
Asked:
rwheeler23
1 Solution
 
Robert SchuttSoftware EngineerCommented:
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
 
rwheeler23Author Commented:
Excellent solution. Thank you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now