I have the following (simplified) result from SELECT * FROM table ORDER BY tick,refid:
tick refid value
----------------
1 1 11
1 2 22
1 3 33
2 1 1111
2 3 3333
3 3 333333
Note the “missing” rows for refid 1 (tick 3) and refid 2 (ticks 2 and 3)
If possible, how can I make a query to add these missing rows using the most recent prior value for that refid? “Most recent” means the value for the row with the same refid as the missing row and largest tick such that the tick is less than the tick for the missing row. e.g.
tick refid value
----------------
1 1 11
1 2 22
1 3 33
2 1 1111
2 2 22
2 3 3333
3 1 1111
3 2 22
3 3 333333
Additional conditions:
- All refids will have values at tick=1.
- There may be many ‘missing’ ticks for a refid in sequence, (as above for refid 2).
- There are many refids and it’s not known which will have sparse data where.
- There will be many ticks beyond 3, but all sequential. In the correct result, each refid will have a result for each tick.
- Missing rows are not known in advance – this will be run on multiple databases, all with the same structure, and different “missing” rows.
I’m using MySQL and cannot change db just now. Feel free to post answer in another dialect, to help discussion, but I’ll select an answer in MySQL dialect over others.
Yes, I know this can be done in the code, which I’ve implemented. I’m just curious if it can be done with SQL.
What
valueshould be returned when a given tick-refid combination does not exist? In this solution, I simply returned the lowest value for that given refid.Revision
I’ve updated the logic to determine what value to use in the case of a null. It should be noted that I’m assuming that ticks+refid is unique in the table.