I have this use case where I have to do incremental aggregation of data that comes in.
I have a table A which has the following columns
id,insertion_time,key,col2,col3
i have to calculate result = col2*col3 for each key and find the max in an hour and put to another table B with columns.
key,hour,max
so the condition is, if I am going to run the same procedure after a while, if new data has been inserted into the table A, the procedure should loop through only the new records to process.
What I have done till now :
I have created a temporary table C to store the last insertion_timestamp that was processed as last_timestamp. Each procedure execution will check for this value and will select only the entries which have insertion_time > last_timestamp.
then the proc will do the following:
get the entry last_timestamp from table C
select the maximum value of col1*col2 group by key and hour where insertion_timestamp > lasttimestamp
for each result check if an entry for (key,hour) pair is present in B
a) if present - update the entry if new col1*col2 value is greater than existing value
b) if not, insert the entry
Is this the right way of doing incremental aggregation in relational databases? Or is there any better approach?
Thanks