| Author |
Topic |
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-10-12 : 16:29:18
|
| [code]/*--I am having some issues with a query wher I need to do the following without a cursor or a looping clause. I want to take the AMount in As illustrated below I have two tables. #PaymentItems is my table that stores a payment and it joins the invoiceItems table using invoiceID field. I need to apply the payment to each line Item until the amount is used up. If there is money left over it goes into the amount Remaining column in PaymentItemsHere's the sql..*/Create Table #InvoiceItems(InvoiceID int, Rate decimal 5,2),PaidAmount Decimal(5,2))insert into #InvoiceItems(InvoiceID, Rate)select 1,10.00Union Allselect 1,11.00Union Allselect 1,12.00Union Allselect 1,14.00Unionselect 2,10.00Union Allselect 2,11.00Union Allselect 2,12.00Union Allselect 2,14.00Create Table #PaymentItems(PaymentID int,InvoiceID int,Amount decimal(5,2),AmountRemaining decimal(5,2))Insert into #PaymentItems(PaymentID,InvoiceID,Amount) select 31321,1,80Union allselect 31322,2,18/*My desired results would be this for :InvoiceITems INvoiceID|Rate|PaidAmount1,10,101,11,111,12,121,13,131,14,142,10,102,11,82,12,02,13,02,14,0see how on invoice 2 only $18 could be applied so after the (10 and 8) were applied the remaing items were 0 because there was nothing left of the payment.and this for :PaymentItemsPaymentID|InvoiceID|Amount|AmountRemaining31321,1,80,3331322,2,18,0Here you can see that there was $33 left from the $80 on payment 1, but nothing left of the $18 on payment 2.--I wrote the following query but it is not working Select *,Row_Number() Over (Partition By InvoiceID Order by Rate Desc) as RowIDinto #TmpInvoiceItemsfrom #INvoiceITemsUpdate aSet a.Paidamount = case When b.Amount - (Select Sum(coalesce(PaidAmount,0)) from #TmpInvoiceItems aa Where aa.InvoiceID = a.InvoiceID and aa.RowID <= a.RowID) > a.Rate then a.Rate else b.Amount - (Select Sum(coalesce(PaidAmount,0)) from #TmpInvoiceItems aa Where aa.InvoiceID = a.InvoiceID and aa.RowID <= a.RowID) end From #TmpInvoiceItems aInner Join #PaymentItems bon a.InvoiceID = b.InvoiceIDUpdate aset AmountRemaining = a.Amount - (Select Sum(coalesce(PaidAmount,0)) from #TmpInvoiceItems aa Where aa.InvoiceID = a.InvoiceID)from #PaymentItems a--(The case statment is not evaluating properly). Any sugestions.*/[/code] |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-10-13 : 15:47:40
|
It's working using the following code. Any sugestions on ways to improve would be appriciated.drop table #InvoiceItemsDrop Table #PaymentItemsDrop Table #TmpInvoiceItemsCreate Table #InvoiceItems(InvoiceID int, Rate decimal (5,2),PaidAmount Decimal(5,2))insert into #InvoiceItems(InvoiceID, Rate)select 1,10.00Union Allselect 1,11.00Union Allselect 1,12.00Union Allselect 1,14.00Unionselect 2,10.00Union Allselect 2,11.00Union Allselect 2,12.00Union Allselect 2,14.00Create Table #PaymentItems(PaymentID int,InvoiceID int,Amount decimal(5,2),AmountRemaining decimal(5,2))Insert into #PaymentItems(PaymentID,InvoiceID,Amount) select 31321,1,80Union allselect 31322,2,18select * into #TmpInvoiceItemsFrom(Select *,Row_Number() Over (Partition By InvoiceID Order by Rate Desc) as RowIDfrom #INvoiceITems) aorder by a.InvoiceID,a.RowiDupdate aSet a.Paidamount = case when b.Amount >= coalesce((Select Sum(Rate) from #TmpInvoiceItems aa Where aa.InvoiceID = a.InvoiceID and aa.RowID <= a.RowID),0) then a.Rate when b.amount - coalesce((Select Sum(Rate) from #TmpInvoiceItems aa Where aa.InvoiceID = a.InvoiceID and aa.RowID < a.RowID),0) > 0 then b.amount - coalesce((Select Sum(Rate) from #TmpInvoiceItems aa Where aa.InvoiceID = a.InvoiceID and aa.RowID < a.RowID),0) else 0 EndFrom #TmpInvoiceItems aInner Join #PaymentItems bon a.InvoiceID = b.InvoiceIDUpdate aset AmountRemaining = a.Amount - (Select Sum(coalesce(PaidAmount,0)) from #TmpInvoiceItems aa Where aa.InvoiceID = a.InvoiceID)from #PaymentItems aselect * from#TmpInvoiceItems aInner Join #PaymentItems bon a.InvoiceID = b.InvoiceID |
 |
|
|
|
|
|