all groups > sql server programming > february 2006 >
You're in the

sql server programming

group:

suggest on sub query improvement.



Re: suggest on sub query improvement. Kasper Birch Olsen
2/23/2006 12:00:00 AM
sql server programming: Order by is only for ordering the final result, you cant us it to to get the
newest 140..

It looks to me like you are trying to the all the tubles, except the 140
newest. Is that right?


[quoted text, click to view]

Re: suggest on sub query improvement. Roy Harvey
2/23/2006 12:00:00 AM
On Thu, 23 Feb 2006 12:02:38 +0100, "Kasper Birch Olsen"
[quoted text, click to view]

When they added TOP to T-SQL, they added new uses for the ORDER BY
clause. Here is the same structure on a table you can experiment
with:

select id
from sysobjects
where id <
(select min(id) from
(select top 14 id from sysobjects order by id desc)
as ss)
order by id

Re: suggest on sub query improvement. Roy Harvey
2/23/2006 12:00:00 AM
It looks fine to me. The only thing to keep in mind is that if there
can be multiple rows with the same DateAdded value - and value 140 is
not unique - there will be over 140 rows that you do not pick up.

Roy


[quoted text, click to view]
RE: suggest on sub query improvement. ML
2/23/2006 2:32:02 AM
Please explain what your actual goal is. Posting DDL, sample data and
expected results might also help.


ML

---
suggest on sub query improvement. guoqi zheng
2/23/2006 4:12:36 AM
Hi,

I have a SQL statement which myself think might be a little tricky. any
suggestion on improvement?

SELECT * FROM tblMsg
WHERE DateAdded <
(SELECT MIN(DateAdded) FROM
(SELECT TOP 140 DateAdded FROM tblMsg Order by DateAdded DESC)
as sss))

Any suggestion on this?

regards,

Guoqi Zheng
AddThis Social Bookmark Button