Sunday, May 1, 2011

SQL strict enforce of order by a date column

I have a table(say TableA) with the following schema

A(int)    B(int)    D (varchar)    C(date)

If I write the query

select A, C from TableA where A >=0 order by A asc, B asc, D asc, C asc.

The last row of the query will have the C date column which is largest ( I mean here the latest which is greater than all the other dates). Is that correct? I have verified with a small query to the table but wanted to verify, to enforce a strict ordering according to the date query is this the only option

select A, C from TableA where A >=0 order by C asc.
From stackoverflow
  • The order-by precedence is left to right so, if you want date to be the major sort field, the following is required.

    select A, C from TableA where A >=0 order by C asc, A asc, B asc, D asc
    

    That second query you gave does it in date order, but you've lost all the other sort criteria.

0 comments:

Post a Comment