![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
The only problem is this: How do I fill in the "refers to" field on the original transaction? |
|
Subsequently it's quite easy as I have to do a read-modify-right .. write to do an amendment or a void. It's the first record, where I need to set "refers to" to be the same as the value generated by the autoincrement, that worries me. I could insert the record with "refers to" zero, then immediately use LAST_INSERT_ID() to amend it, but I feel sure there ought to be a way of doing this in one query. |
#3
| |||
| |||
|
|
On Sat, 30 Oct 2010 12:18:51 GMT, Robert Billing wrote: The only problem is this: How do I fill in the "refers to" field on the original transaction? You may wish to simply leave this null instead. Are there business rules that require this to refer the record to itself? |
#4
| |||
| |||
|
|
The hyperspace communicator crackled into life and we heard Peter H. Coffin say: On Sat, 30 Oct 2010 12:18:51 GMT, Robert Billing wrote: The only problem is this: How do I fill in the "refers to" field on the original transaction? You may wish to simply leave this null instead. Are there business rules that require this to refer the record to itself? The way I saw it was that it would be useful to be able to do a GROUP BY on the refers field to get the parent record and all its children. |
|
Similarly sorting on the refers field and then the sequence would produce the original record, followed by the changes. This is something I will need to generate as a report. |
#5
| |||
| |||
|
|
The only complication is that some transactions are amendments to previous ones, either changing a transaction or voiding it completely. In this case I want to write another record, but leave the original one in place, so that I have an audit trail of changes. What I was thinking of doing was having two fields. The transaction number autoincrements as new records are added, but there is also a "refers to" field which, if the record is an amendment, contains the transaction number of the transaction being amended. The original transaction has the same number in both fields. Now I can select original transactions on the criterion that transaction=refers, and do a strawberry query to find the last amended form of any transaction. This, plus a filter to remove voids, produces my customer statements. The only problem is this: How do I fill in the "refers to" field on the original transaction? |
#6
| |||
| |||
|
|
Okay, the nice thing is that both the null and the refers-to being the same *mean* the same thing. So it doesn't matter much which it is, and so long as there's no other condition that would make this happen, you can flip between them at need. For example, the records can start out as null-parent, then you can UPDATE my_table SET p_id = id WHERE p_id IS NULL and do the reports, then UPDATE mytable SET p_id = NULL WHERE p_id = id to put it back if you want. This is a little messy but it might make up for it in the sense that while you're running the reports, it can still be active and doing it's thing but you've got the state of it saved AS OF WHEN YOU STARTED THE REPORTING. That is, any new records will show up null-parent instead of self-parent, regardless of how long you're fussing around with the reporting, so long as you only count stuff that has a not-null parent. |
#7
| |||
| |||
|
|
The hyperspace communicator crackled into life and we heard Peter H. Coffin say: On Sat, 30 Oct 2010 12:18:51 GMT, Robert Billing wrote: The only problem is this: How do I fill in the "refers to" field on the original transaction? You may wish to simply leave this null instead. Are there business rules that require this to refer the record to itself? The way I saw it was that it would be useful to be able to do a GROUP BY on the refers field to get the parent record and all its children. Similarly sorting on the refers field and then the sequence would produce the original record, followed by the changes. This is something I will need to generate as a report. |
#8
| |||
| |||
|
|
Just do a GROUP BY or ORDER BY based on COALESCE(refer,transaction). That expression will return refer if not null, and transaction otherwise. Cheers Tony |
![]() |
| Thread Tools | |
| Display Modes | |
| |