Link to home
Create AccountLog in
Avatar of elliottbenzle
elliottbenzle

asked on

Can you use a primary and secondary "order by" in SQL to arrange rows in a recordset?

I'm using ASP/MS access/dreamweaver.

I have a table which contains two fields, one with the date and one with the time. The order is all mixed up. Right now my SQL orders the data according to the date.

"SELECT * FROM hilliardmicroschedule ORDER BY vardate ASC"

But I'm running into the problem that when the dates are the same the times are still all mixed up. Is there a way to alter the SQL so that it orders by vardate first and then orders by vartime. The resulting recordset should be ordered primarily by date and when the dates are the same it will order by time. I'm assuming the code would look something like this:

"SELECT * FROM hilliardmicroschedule ORDER BY vardate ASC (AND ORDER BY vartime ASC)"

You can see the problem on this page under the first schedule (hilliard micro-mini) where the date order is fine but the times are all mixed up.

http://www.glowfishtw.com/flagfootball/schedules.asp?reg=hilliard

Thanks for any help.
ASKER CERTIFIED SOLUTION
Avatar of Haris Dulic
Haris Dulic
Flag of Austria image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Samo's got it, you can put multiple sub orderings separated by commas after ORDER BY and of course ignore the double quotes and ending parenthesis...  You also don't need to specify ascending as this is the default but can put DESC after each item in the list.

SELECT * FROM hilliardmicroschedule ORDER BY vardate, vartime

or if you wanted the time in reverse within each vardata which was ascending:

SELECT * FROM hilliardmicroschedule ORDER BY vardate, vartime DESC