Link to home
Start Free TrialLog in
Avatar of pvsbandi
pvsbandiFlag for United States of America

asked on

SQL question

We are on SQL Server 2012.
We have three tables. TB_MASTER, TB_DETAIL and TB_FINAL
   
TB_MASTER	
OWNER_ID	OVER_AMOUNT_NO
123	      400
234	      300

TB_DETAIL
OWNER_ID	WORKER_ID	tobe_paid_AMOUNT_NO
123	  555	             200
123	  666	             300
234	  777	             200

Open in new window


I want the TB_FINAL to have the inserted rows and TB_MASTER to have updated rows as below:
TB_FINAL		
OWNER_ID	WORKER_ID	tobe_paid_AMOUNT_NO
123	555	              0
123	666	             100
234	777	             100

TB_MASTER
OWNER_ID	OVER_AMOUNT_NO
123	     0
234	     200

Open in new window


The explanation to the desired output is as follows:
* Owner 123 owes 400 to the agency and so, the amount to be paid 200, to worker  555 will be deducted from 400.
* After the above deduction, Owner 123 still owes 200 (400-200). Since Worker 666 needs to be paid 300, he will be paid 100 (300 - 200).
* Owner 234 owes 300 to the agency and the worker 777 is to be paid 200. So, worker 234 gets paid only 100.
* TB_MASTER table needs to update the outstanding overpayment for the Owners.

Can someone let me know of an easy way of achieving this?
Avatar of Mark Wills
Mark Wills
Flag of Australia image

Back in a moment, will populate some test data and go through the different ways...
Ummm, not sure i understand the maths.

234 has $300
234 Owes 777 $200

Therefore FINAL should be
 234 777  $0

and Master should be
234 $100

Am I missing something ?
Avatar of pvsbandi

ASKER

Hi Mark,

   This owing the amount, is to the Agency; not to the workers.
So, when the agency is paying the workers, it will look at the existing overpayment and deduct from the amount to paid to the workers and pay the rest to them.
OK, that clarifies worker 666 - the agency is kicking in the shortfall from overpaid to agency (as shown in Master)

But if 234 has overpaid $300, and the claim from 777 is only $200, then how do we get the agency paying $100 ?

Maybe 234 had overpay of $200 and 777 was owed $300 - then it makes sense.

Apologies for the confusion....
You are right. My apologies for the confusion.
For worker 777, shouldn't get paid anything and the outstanding overpayment for worker 234 should be 100
So what is the final OUTPUT for TB_FINAL and TB_MASTER?
ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you both!
A pleasure to have been able to help.

Just a quick note, be careful with recursive CTE's they can get a bit 'greedy' for resources. Also note that you may need the maxrecursion 0 option, using some of my dummy data, it generated errors.

This is nothing against Pawan's solution, I was working on a similar approach until I saw the SQL2012. More so, just a word of warning about recursion.

Cheers, and happy SQL coding :)
Thanks Mark!