Pair Columns then ORDER BY

This HAS to be a PEBCAK problem as it seems so basic. Is there a query to say, “If ‘mon’ = 1 THEN ORDER BY ‘time’ THEN if ‘tue’ = 1 ORDER BY ‘time’, THEN if ‘wed’ = 1 ORDER BY ‘time’” ?

+=====+=====+=====+========+
| mon | tue | wed |  time  |
+=====+=====+=====+========+
|  0  |  1  |  1  |  0100  |
|  0  |  0  |  0  |  2200  |
|  1  |  1  |  1  |  1500  |
|  1  |  1  |  0  |  0600  |
|  0  |  0  |  0  |  1400  |
|  1  |  0  |  1  |  1100  |
+=====+=====+=====+========+

Desired result:

mon 0600
mon 1100
mon 1500
tue 0100
tue 0600
tue 1500
wed 0100
wed 1100
wed 1500

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

...
ORDER BY CASE WHEN mon=1 THEN 1 WHEN tue=1 THEN 2 WHEN wed=1 THEN 3 END,
    time


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x