Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

T SQL help

$
0
0

Hi All,

Need help on following scenario.

 DECLARE @ProjectInvoice TABLE(InvoiceID INT, ProjectID INT, InvoiceAmount float)
 DECLARE @ProjectBudget TABLE(ProjectBudgetID INT, ProjectID INT, BudgetAmount float)
  INSERT INTO @ProjectBudget VALUES(11,1,100000),(12,1,50000)
INSERT INTO @ProjectInvoice VALUES (12345,1,25000)
,(12346,1, 30000) 
,(12347,1, 40000) 
,(12348,1, 30000)
,(12349,1, 10000)

select * from @ProjectBudget
select * from @ProjectInvoice
	
-- Expected Result
--InvoiceID ProjectID InvoiceAmount ProjectBudgetID BugetAmountLeft 
--  12345	1		25k		11		 75k  -- > 100k(original budget) - 25k   
--  12346	1		30k		11		 45k --> 75k( budget amount left after first invoice) - 30k
--  12347	1		40k		11		 5k	--> 45k - 40k
--  12348	1		5k		11		 0k	--> now only 5k is left budget, whereas we have 30k to deduct, deduct 5k from projectBudget11 
--  12348	1		25k		12		 25k --> and the rest 25k from project Budget 12
--  12349 	1		10k		12		 15k

There can many be projectID's not just 1, but the same logic applies for all projectId's.

It would be great, if some one helps me out with this.

Thanks!

Mannaha


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>