Monday, March 19, 2012

last n records simpleand easy

how do i display last n record on 6.5
if i have a primary key(number)select pkey
, foo
from yourtable xxx
where n > (
select count(*)
from yourtable
where foo > xxx.foo )
"foo" is the column that determines the sequence

without a sequence, "top" has no meaning|||You mean an identity Column?

SELECT TOP n * FROM yourTable Order by PKID DESC|||Brett, when did sql server add support for TOP

7, wasn't it?

;) ;)|||doooooooh

Details...details...

Yup no TOP|||No i mean a primary key which is a number

select top 300 * from tablename
order by columnname desc (columnname is primary key)

Primary key is smallint

when i do

select clientid, foo
from client x
where 300>( select count(*)
from client
where foo> x.foo)

It gives error foo column not there

here after 6.5 sql became easy|||sjumma, what are the columns in your table?

which one do you want to sort by?|||let say i want 1 column

select clientid
from client

clientid is smallint and contunious number (1,2,3,...)

how will i make just the last 300 records be displayed|||In MS-SQL 6.5, I'd use:SET ROWCOUNT 300

SELECT clientid
FROM dbo.client AS a
ORDER BY clientid DESC

SET ROWCOUNT 0Note that an index on client.clientid will help performance immensely.

-PatP|||<sigh />

oh yeah, rowcount :p

select clientid
from client x
where 300>( select count(*)
from client
where clientid> x.clientid)|||Originally posted by r937
oh yeah, rowcount :p Mondo way ugly, but it gets an answer while I'm still young enough to care! I'd hate to contemplate how long it would take to slog the count query through a half million clients, even though it is esthetically more pleasant than the ROWCOUNT solution!

-PatP

No comments:

Post a Comment