I have a one to many relationship between the Account table and a ScheduledPayment table. One account can have many payments but there should be a maximum of one payment per day.
Although unlikely, there are two scenarios which I need to cater for...
If there is one or more
Paymentrecords for anAccountIdon a day, with different payment and or reference then all payments should be marked as cancelled.If there is more than one
Paymentrecord for anAccountIdon a day, with the same payment and reference then one record should be the winner and the other be marked as cancelled.
In reality, I don't anticipate seeing this scenario all that much because it is down to a concurrency issue in code, if two users are working on the same record at the same time.
Any ideas would be greatly appreciated!
Account
---------
AccountId
---------
1
2
3
4
ScheduledPayment
----------------
ScheduledPaymentId | AccountId | Amount | Reference | State
------------------------------------------------------------
1 | 1 | 100 | ABCDE | Live
2 | 1 | 100 | ABCDE | Live (For these two, 1 would the winner and two marked as cancelled)
3 | 2 | 100 | ABCDE | Live
4 | 2 | 110 | ABCDE | Live
5 | 2 | 110 | ABCDE | Live
6 | 2 | 130 | ABCDE | Live (For these, all would be marked as cancelled)
7 | 3 | 100 | ABCDE | Live
8 | 4 | 100 | ABCDE | Live
Paymenttable to avoid these types of duplicates.