Unix Technical Forum

sorting twice?!

This is a discussion on sorting twice?! within the SQL Server forums, part of the Microsoft SQL Server category; --> here's a good one for you... I want to return the last 20 records I have modified. I have ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 07:47 PM
Tim
 
Posts: n/a
Default sorting twice?!

here's a good one for you...

I want to return the last 20 records I have modified. I have a
datemodified field - excellent.
So I run a query to select the top 20 when ordered by datemodified
desc.
But now I have these results I want them sorted by companyname.

Is this possible?

Yes I could use my GUI to do the second sort, but can it be done just
in a query?

Thanks

Tim

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 07:47 PM
Jack Vamvas
 
Posts: n/a
Default Re: sorting twice?!

Yes. Something like : SELECT myColID FROM myTable
WHERE myColID IN (SELECT TOP 20 FROM myTable ORDER BY dateDESC)
ORDER BY companyName ASC

--
----
Jack Vamvas
___________________________________
Receive free SQL tips - www.ciquery.com/sqlserver.htm
___________________________________


"Tim" <Citizen10Bears@gmail.com> wrote in message
news:1151401355.662526.118990@75g2000cwc.googlegro ups.com...
> here's a good one for you...
>
> I want to return the last 20 records I have modified. I have a
> datemodified field - excellent.
> So I run a query to select the top 20 when ordered by datemodified
> desc.
> But now I have these results I want them sorted by companyname.
>
> Is this possible?
>
> Yes I could use my GUI to do the second sort, but can it be done just
> in a query?
>
> Thanks
>
> Tim
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 07:47 PM
Erland Sommarskog
 
Posts: n/a
Default Re: sorting twice?!

Tim (Citizen10Bears@gmail.com) writes:
> here's a good one for you...
>
> I want to return the last 20 records I have modified. I have a
> datemodified field - excellent.
> So I run a query to select the top 20 when ordered by datemodified
> desc.
> But now I have these results I want them sorted by companyname.
>
> Is this possible?


Sure. With derived tables (almost) everything is possible:

SELECT ...
FROM (SELECT TOP 20 ...
FROM tbl
ORDER BY datemodified DESC) AS x
ORDER BY companyname

A derived table is a verital temp table within the query so speak, and
is an immensly powerful tool to build complex queries. Important to know
is that the optimizer is very good at recasting computation order to get
better performance. (Although in this particular case it is not likely
to happen because of the TOP operator.)


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 07:47 PM
Tim
 
Posts: n/a
Default Re: sorting twice?!

thanks guys.

Tim

Erland Sommarskog wrote:
> Tim (Citizen10Bears@gmail.com) writes:
> > here's a good one for you...
> >
> > I want to return the last 20 records I have modified. I have a
> > datemodified field - excellent.
> > So I run a query to select the top 20 when ordered by datemodified
> > desc.
> > But now I have these results I want them sorted by companyname.
> >
> > Is this possible?

>
> Sure. With derived tables (almost) everything is possible:
>
> SELECT ...
> FROM (SELECT TOP 20 ...
> FROM tbl
> ORDER BY datemodified DESC) AS x
> ORDER BY companyname
>
> A derived table is a verital temp table within the query so speak, and
> is an immensly powerful tool to build complex queries. Important to know
> is that the optimizer is very good at recasting computation order to get
> better performance. (Although in this particular case it is not likely
> to happen because of the TOP operator.)
>
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
>
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pro...ads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodinf...ons/books.mspx


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:48 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com