Sort Records in Original Order After UNION ALL My question seems to have been worded poorly the first time around, so this is my attempt to explain it more clearly + provide table structure and data below.
Please consider table dt:
Create Table dt
(LogID Int Not Null Identity Primary Key, StartTime DateTime, EndTime DateTime)

Insert Into dt(StartTime,EndTime)
Values 
    ('2019-07-31 07:00', '2019-07-31 7:30'),
    ('2019-07-31 06:30', '2019-07-31 07:00'),
    ('2019-07-31 12:00', '2019-07-31 13:15'),
    ('2019-07-31 7:30', '2019-07-31 12:00'),
    ('2019-7-31 05:30', '2019-07-31 06:30')





LogID
StartTime
EndTime




1
2019-07-31 07:00:00.000
2019-07-31 07:30:00.000


2
2019-07-31 06:30:00.000
2019-07-31 07:00:00.000


3
2019-07-31 12:00:00.000
2019-07-31 13:15:00.000


4
2019-07-31 07:30:00.000
2019-07-31 12:00:00.000


5
2019-07-31 05:30:00.000
2019-07-31 06:30:00.000




I'm trying to come up with a query that would return

each record twice, once with StartTime and once with EndTime as EventTime
Order by EventTime and then by StartTime first, EndTime second.

Desired result:




LOGID
EVENTTIME




5
31/07/2019 5:30


5
31/07/2019 6:30


2
31/07/2019 6:30


2
31/07/2019 7:00


1
31/07/2019 7:00


1
31/07/2019 7:30


4
31/07/2019 7:30


4
31/07/2019 12:00


3
31/07/2019 12:00


3
31/07/2019 13:15




I have tried
Select 
LogID, StartTime As EventTime, 'Start' As EventType from dt
Union All
Select LogID, EndTime, 'End'from dt
Order By
EventTime ASC, EventType Desc

But the order is not correct
Thank you for your help!
How about:
select LOGID, DESCRIPTION, STARTTIME Time from T
union all 
select LOGID, DESCRIPTION, ENDTIME from T
order by LOGID, Time

If you want to order the event pairs by STARTTIME instead of LOGID, then
with q as
(
    select LOGID, DESCRIPTION, STARTTIME, STARTTIME Time from T
    union all 
    select LOGID, DESCRIPTION, STARTTIME, ENDTIME Time from T
)
select LOGID, DESCRIPTION, Time
from q
order by STARTTIME, Time