Tmiskiewicz
Tmiskiewicz

Reputation: 403

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 !

Upvotes: 0

Views: 91

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269563

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;

Upvotes: 2

The Impaler
The Impaler

Reputation: 48770

A simple ORDER BY will suffice:

select * from my_table order by `date`, hour;

Please note the date is usually a reserved word, so its usage is discouraged, and needs to be quoted.

Upvotes: 0

Related Questions