Skip to content
Advertisement

How to add date rows for messages query?

I got a Messages table.

I want to insert date rows and a type column based on the date, so it looks like this.

When ordering by date, type, the first and the last rows are dates. And there is a date row between every two messages with different dates having the later date’s value.

I got no idea how to tackle this one. Please tell me if you got any ideas on how to approach this.

Advertisement

Answer

This is quite complicated, because you want the new rows to contain the next date but the previous max id (if it exists) and also 1 row at the end.
So you can use UNION ALL for 3 separate cases:

Note that this will work only if your dates are in the format YYYY-MM-DD which is comparable and the only valid date format for SQLite.

See the demo.
Results:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement