April 7, 2009 at 1:44 am
Good morning ,
I trying to get by SQL Query on SQL 2005 , the Last record in one table
& last record -1 - the record that before last one -
(which is so important to me )
can you help me please , will appreciate it
April 7, 2009 at 2:07 am
If you have some counter (identity..etc) in the table you can do this. Lets say the table
create table test (nID int, name varchar(100))
insert into test (nid,nome) values (1,'Row1')
insert into test (nid,nome) values (2,'Row2')
insert into test (nid,nome) values (3,'Row3')
WITH LastRow AS
(SELECT ROW_NUMBER() OVER (ORDER BY nid desc) AS ROWID, * FROM test)
SELECT * FROM lastrow where ROWID = 1 or ROWID = 2
or
WITH LastRow AS
(SELECT top 2 ROW_NUMBER() OVER (ORDER BY nid desc) AS ROWID, * FROM test)
SELECT * FROM lastrow
or
select top 2 * from test order by nid desc
This second example its better if you have a large table.
this return
Rowid Nid Name
13Row3
22Row2
if this example dont works to you, put the table structure to us.
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply