I have a database where I store objects. I have the following (simplified) schema
CREATE TABLE MyObjects ( UniqueIdentifier Id; BigInt GenerationId; BigInt Value; Bit DeleteAction; )
Each object has a unique identifier (‘Id’), and a (set of) property (‘Value’). Each time the value of the property for an object is changed, I enter a new row into this table with a new generation id (‘GenerationId’, which is monotonically increasing). If an object is deleted, then I record this fact by setting the ‘DeleteAction’ bit to true.
At any point in time (generation), I would like to retrieve the state of all of my active objects!
Here’s an example:
Id GenerationId Value DeleteAction 1 1 99 false 2 1 88 false 1 2 77 false 2 3 88 true
Objects in generations are:
1: 1 {99}, 2 {88} 2: 1 {77}, 2 {88} 3: 1 {77}
The key is: how can I find out the row for each unique object who’s generation id is closest (but not exceeding) to a given generation id? I can then do a post-filter step to remove all rows where the DeleteAction field is true.
Here’s the working version: