I have a query that currently brings up data like this:
ID | Name | Arrival | Departure
-----------------------------------
1 | John | NULL | 2:30:00
2 | John | NULL | 11:00:00
3 | John | NULL | 14:00:00
4 | John | 10:30:00 | 11:00:00
5 | John | 12:00:00 | 13:00:00
This is when I do: order by Name, Arrival, Departure
.
However, what I would like instead is the following:
- If the Arrival entry is
NULL
, then rows should be ordered by the Departure column. - If the Arrival entry is
NULL
AND there are multiple rows with the same Departure data, then it should be ordered after the other rows that have an Arrival entry.
Result:
ID | Name | Arrival | Departure
-----------------------------------
1 | John | NULL | 2:30:00
4 | John | 10:30:00 | 11:00:00
2 | John | NULL | 11:00:00
5 | John | 12:00:00 | 13:00:00
3 | John | NULL | 14:00:00
Edit: For those who thought this is a duplicate question - originally the questions are not the same due to the second condition. However, because of the way my data is structured (Arrival times are always before Departure times), the same answer is applicable.
Edit 2: Departure can be NULL as well. When Departure is null, the Arrival entry is non-null. If this is the case, it should be ordered by Arrival. If two rows both have the same Arrival entry, then NULLs should be BEFORE non-nulls. Note this is opposite to NULL Arrival entries (condition 2 above), where NULLs are after non-nulls.