I have a table with a timestamp column, something like this:
╔═══════╦════════════╦
║ name ║ date_time ║
╠═══════╬════════════╬
║ A ║ 100 ║
║ B ║ 110 ║
║ C ║ 120 ║
║ D ║ 140 ║
║ E ║ 180 ║
║ F ║ 190 ║
╚═══════╩════════════╩
I need to return the records so that the records come in two groups, the records where date_time is in the future are the first group, and the records where date_time is in the past are the second group. The records in the future need to be sorted in ascending order(i.e. the closest to now comes first), and the records in the past need to be sorted in descending order.
I managed to solve this by making two separate queries and joining the results with union all, but this is not very performant and I was curious to know if there was any better approach? Maybe by using a conditional sorting on the order by?