13

I want to sort according to date first and then if date is similar then according to id..How to do that in Informix/HSQL query?

coderslay
  • 13,960
  • 31
  • 73
  • 121

6 Answers6

30
SELECT FIELD1, FIELD2 FROM TABLE ORDER BY FIELD1 ASC, FIELD2 ASC

A good tutorial on this SQL ORDER BY

ChrisBint
  • 12,773
  • 6
  • 40
  • 62
5

This should work:

SELECT * FROM Table
ORDER BY date, id;
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
4

Does

[rest of query] order by date, id

work?

YXD
  • 31,741
  • 15
  • 75
  • 115
3
select * from (select * from tablename order by col1) AS T order by col2

(It's necessary to give an alias to the virtual table, hence "AS T")

Fattie
  • 27,874
  • 70
  • 431
  • 719
Rohit SV
  • 31
  • 1
3

Try this(adjusted to your needs):

SELECT * FROM table ORDER BY datecol ASC, id ASC
fyr
  • 20,227
  • 7
  • 37
  • 53
0
select * from (select * from tablename order by col1) AS T order by col2

This works but is redundant and obtuse. Note the subquery (on "col1") becomes the secondary sort; the primary sort is the encompassing query. Some environments may require the "as T" aliasing of the subquery, but not all.

select * from tablename order by col2, col1

This does the same thing as above, with simple clarity

Or for a more realistic example:

select * from customers order by lastname, firstname
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109