Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Fin. Adjustment

Author  Topic 

khalik
Constraint Violating Yak Guru

443 Posts

Posted - 2009-10-29 : 10:43:49
Hi Guys,
its been long time for me. I have a fin problem. below is the tables structure and problem

PartyInvoice Table
PartyID InvoiceNumber Amount
A Invoice001 100
A Invoice002 200
B Invoice003 250

PartyPayment tables
PartyID PaymentNumber Amount
A Pay001 150
B Pay002 50



we have to adjust the payment againt the invoices. expected output is

Results
PartyID InvoiceNumber InvAmount PendingAmount
A Invoice001 100 0
A Invoice002 200 150
B Invoice003 250 200


I have SP which does exactly but looping which i want to avoid. any simple way to do the same in batch processing.
Thanks in advance

========================================
Project Manager who loves to code.
===============
Ask to your self before u ask someone

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-10-29 : 12:39:52
u need sifu or peso or madhi or webby or tg or tara or anyone else than me...


Hope can help...but advise to wait pros with confirmation...
Go to Top of Page

khalik
Constraint Violating Yak Guru

443 Posts

Posted - 2009-10-29 : 13:04:12
ya ya wating for all those guys... hope this thread will be up till that time...

========================================
Project Manager who loves to code.
===============
Ask to your self before u ask someone
Go to Top of Page

khalik
Constraint Violating Yak Guru

443 Posts

Posted - 2009-10-30 : 06:20:21
Can this be done with out looping? i think they shd be a way out....

========================================
Project Manager who loves to code.
===============
Ask to your self before u ask someone
Go to Top of Page

khalik
Constraint Violating Yak Guru

443 Posts

Posted - 2009-10-30 : 10:41:01
Will recursive CTE be the option if so how?

========================================
Project Manager who loves to code.
===============
Ask to your self before u ask someone
Go to Top of Page

khalik
Constraint Violating Yak Guru

443 Posts

Posted - 2009-11-04 : 10:07:02
Is there no way to avoid loop. i have2 loops
first for customers and the second for the payment made. adjust the payment made against the invoices.

clues or tips welcome...

========================================
Project Manager who loves to code.
===============
Ask to your self before u ask someone
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2009-11-04 : 13:24:28
Here is one way. There are probably better ways, but I had a spare minute... :)
-- Set up sample data
CREATE TABLE #PartyInvoice (PartyID CHAR(1), InvoiceNumber VARCHAR(50), Amount MONEY)
INSERT #PartyInvoice
SELECT 'A', 'Invoice001', $100
UNION ALL SELECT 'A', 'Invoice002', $200
UNION ALL SELECT 'A', 'Invoice003', $400
UNION ALL SELECT 'B', 'Invoice003', $250

CREATE TABLE #PartyPayment (PartyID CHAR(1), PaymentNumber VARCHAR(50), Amount MONEY)
INSERT #PartyPayment
SELECT 'A', 'Pay001', 150
UNION ALL SELECT 'B', 'Pay002', 50

-- Creae CTE
;
WITH Invoice (PartyID, InvoiceNumber, InvoiceAmount, PaymentAmount)
AS
(
SELECT
PartyID,
InvoiceNumber,
Amount AS InvoiceAmount,
Payment.PaymentAmount
FROM
#PartyInvoice AS Invoice
CROSS APPLY
(
SELECT SUM(Amount) AS PaymentAmount
FROM #PartyPayment AS Payment
WHERE Payment.PartyID = Invoice.PartyID
) AS Payment
)

-- Perform Select
SELECT
A.PartyID,
A.InvoiceNumber,
A.InvoiceAmount,
CASE
WHEN SUM(B.InvoiceAmount) - A.PaymentAmount < 0 THEN 0
ELSE SUM(B.InvoiceAmount) - A.PaymentAmount
END AS PendingAmount
FROM
Invoice AS A
LEFT OUTER JOIN
Invoice AS B
ON A.PartyID = B.PartyID
AND A.InvoiceNumber >= B.InvoiceNumber
GROUP BY
A.PartyID,
A.InvoiceNumber,
A.InvoiceAmount,
A.PaymentAmount
ORDER BY
A.PartyID,
A.InvoiceNumber

-- Cleanup
DROP TABLE #PartyInvoice
DROP TABLE #PartyPayment
Go to Top of Page
   

- Advertisement -