Enissay
Enissay

Reputation: 4953

MySQL Advanced Ordering combining two queries

I'm getting the following data using two queries which I'm then using a UNION ALL to combine:

InitialData

However, I'd like the final result to be as follows:

So the final result should be like this:

endResults

Please advise how to proceed.

PS: Please find the data used in my example in this GoogleSheet

Upvotes: 1

Views: 39

Answers (1)

GMB
GMB

Reputation: 222432

I am thinking of an order by clause like:

order by 

    -- if event type is 1, order by SideA, else order by SideB        
    case when EventType = 'IN' then SideA else SideB end,

    -- then event time
    EventTime,

    -- `Out` goes before `IN` if there are several events at the same time
    EventType desc

Upvotes: 3

Related Questions