Skip to content
Advertisement

SQL Order by 2 conditions

Let’s say that I have a database, that looks like that:

price      date       hour
12.00    2018-12-11    5
13.00    2018-12-04    2
14.00    2018-12-06    1 
15.00    2018-12-11    1
16.00    2018-12-04    6
17.00    2018-12-06    10

I need to order by date and if days are the same after hour, so results should be:

   price      date       hour
    13.00    2018-12-04    2
    16.00    2018-12-04    6
    14.00    2018-12-06    1
    17.00    2018-12-06    10
    15.00    2018-12-11    1
    12.00    2018-12-11    5

I tried to write a simple query, but it couldn’t take into account 2 conditions, one after another:

SELECT price, date, hour
FROM table
WHERE date BETWEEN '2018-12-04' AND '2018-12-11'
ORDER BY date, hour

Could anyone help with this issue ?

Thanks All !

Advertisement

Answer

The only real issue I can think of would be if hour were stored as a string. If so, use implicit conversion:

SELECT price, date, hour
FROM table
WHERE date BETWEEN '2018-12-04' AND '2018-12-11'
ORDER BY date, hour + 0;
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement