Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 two column sort

Author  Topic 

BendJoe
Posting Yak Master

128 Posts

Posted - 2008-10-01 : 12:44:13
I need to sort a result set first based on a two columns.

eg;

the two columns are date and month.
if I set order by month desc.
I have hundreds of records for the same month.
So I need to sort by date after words.
How can I do this.

I have Select Date,Month from Table
order by month desc.

I dont know how to add the second column sorting
Please help

raky
Aged Yak Warrior

767 Posts

Posted - 2008-10-01 : 12:51:08
Select Date,Month from Table
order by month desc,date desc

You can specify many columns in order by clause with comma separated
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-01 : 12:51:35
[code]Select Date,Month from Table
order by month desc,date desc[/code]
Go to Top of Page

BendJoe
Posting Yak Master

128 Posts

Posted - 2008-10-01 : 12:52:58
Thanks,

One more question If I have a result set for a query is there a way to make it return the result set in random order each time the query is run.
Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-01 : 12:55:11
quote:
Originally posted by BendJoe

Thanks,

One more question If I have a result set for a query is there a way to make it return the result set in random order each time the query is run.
Thanks


yup you can use

select fields... from table1
join table2
.....
order by NEWID()
Go to Top of Page

BendJoe
Posting Yak Master

128 Posts

Posted - 2008-10-01 : 13:03:00
Thanks
Go to Top of Page
   

- Advertisement -