Groups | Blog | Home
all groups > sql server (microsoft) > may 2007 >

sql server (microsoft) : Many to Many? Maybe Not


strvariant NO[at]SPAM yahoo.com
5/16/2007 11:30:12 AM
I have two tables. One for Payments and one for Transactions The
tables are set up basically like this

Payments

Account Payment_Amount PaymentID
1234 $100.00 1000
1234 $100.00 1001
1234 $100.00 1002


Transactions

Account Transaction_Amount TransactionID
1234 125.00 9999
1234 125.00 9998
1234 125.00 9997

There are no common fields, in either table, other than the Account
number. The payments have to be applied to the transactions until the
payments are exhausted.

The trick here is that when the first payment is applied to the first
transaction there is $25.00 remaining. When this happens the second
payment is applied to the remaining $25.00 first and then to the
second transaction. In this example the second transaction has a
remaining amount of $50.00 which will be used by the third payment and
so on.

This is the end result --- that all of the payments are applied to the
transactions. But you have to be able to tell which payment (or
portion thereof) was applied to which transaction. Any ideas?

Thanks,

strvariant
Ed Murphy
5/16/2007 9:19:04 PM
[quoted text, click to view]

B. Edwards
5/16/2007 9:28:10 PM
tblPayments
Payment_Amount
Payment_Id

tblTransactions
Account
Transaction_Amount
Transaction_Id

tblTransactionsPayments
TransactionId
PaymentID
PaymentAppliedAmount

[quoted text, click to view]

strvariant NO[at]SPAM yahoo.com
5/17/2007 7:54:19 AM
[quoted text, click to view]


tblTransactionsPayments
TransactionId
PaymentID
PaymentAppliedAmount

This seems to address that. I see that if there is a payment amount
left over that a new record would be inserted with the same ID's but
with the remaining amount. I am looking for a way to do this without
using cursors but I am running out of options.

strvariant NO[at]SPAM yahoo.com
5/17/2007 12:32:28 PM
[quoted text, click to view]

Yes, this will work. I'll have to use a cursor to populate the results
but that's okay. As long as it works I can always go back and fine
tune the SQL to speed things up.

Thanks all!

strvariant
AddThis Social Bookmark Button