Renaud is Not Bill Gates
Renaud is Not Bill Gates

Reputation: 2084

Using "Order by" twice in a single Query

I have a table with 2 columns. Date_Réserve(type of Date), Heure_Réserve(type of Time)

What I want to do is select the result ordered by the Date_Réserve and then thie result I get is ordered by Heure_Réserve.

this is the query I tried :

select * from (select Date_Réserve,Heure_Réserve from réserve order by Date_Réserve)t order by Heure_Réserve

But It gives me this error:

The ORDER BY clause is invalid in views, inline functions, derived tables, and subqueries, unless TOP is also specified.

Upvotes: 3

Views: 8816

Answers (6)

psur
psur

Reputation: 4519

List needed columns separated by comma:

select Date_Réserve, Heure_Réserve from réserve
order by Date_Réserve, Heure_Réserve

Upvotes: 5

Richard Vivian
Richard Vivian

Reputation: 1750

You can use an order by in a sub query when you use a TOP statement.

select * from 
(select TOP 100 PERCENT Date_Réserve,Heure_Réserve from réserve order by Date_Réserve) t order by Heure_Réserve

Upvotes: 0

Jodrell
Jodrell

Reputation: 35716

One clause, two parts

select 
            Date_Réserve,
            Heure_Réserve 
    from 
            réserve 
    order by 
             Date_Réserve, 
             Heure_Réserve

--

If this is just an example and you really need to order the results by a column that will be unavailable in the outer query you could do,

select 
            Date_Réserve,
            Heure_Réserve 
    from 
        (
            select 
                    (ROWNUMBER() OVER ORDER BY Date_Réserve) do,
                    Date_Réserve,
                    Heure_Réserve 
                from
                    réserve 
        )
    order by 
             do, 
             Heure_Réserve

but, as you can see, that would be pointless and inappropriate in your scenario.

Upvotes: 1

Diego
Diego

Reputation: 16714

If you want to order by more than one column:

select Date_Réserve,Heure_Réserve
from réserve
order by Date_Réserve, Heure_Réserve

If not, the order in subquery is useless.

Upvotes: 2

rvandoni
rvandoni

Reputation: 3397

try this:

SELECT Date_Réserve, Heure_Réserve FROMréserve ORDER BY Date_Réserve ASC, Heure_Réserve ASC

Upvotes: 3

Oded
Oded

Reputation: 499002

You do not need to use ORDER BY in your sub query - it is meaningless there.

For primary/secondary sort, you use a single ORDER BY:

SELECT Date_Réserve,Heure_Réserve 
FROM réserve 
ORDER BY Date_Réserve, Heure_Réserve

Upvotes: 5

Related Questions