Wednesday, 26 June 2013

FINDING TOP Nth RECORD FROM TABLE WITH OUT CHANGING THE ORDER OF RECORDS INSERTED



HI ALL,

FINDING TOP Nth RECORD FROM TABLE WITH OUT CHANGING THE ORDER OF RECORDS INSERTED (how they inserted in table same way i needed)

I already posted row_number() in my blog to get row number for each record using that logic with the help of sub query i am getting the result.

DATA:

Declare @test table(id int,name varchar(30))
insert into @test values(589,'ravi')
insert into @test values(689,'ramu')
insert into @test values(789,'rahul')
insert into @test values(525,'raghu')
insert into @test values(575,'ramesh')
insert into @test values(532,'ragav')
insert into @test values(556,'raju')
insert into @test values(500,'rita')
insert into @test values(501,'rama')
insert into @test values(505,'RIJJU')--10 Record while inserting the data to table
insert into @test values(589,'rajaini')
insert into @test values(456,'ranjith')
insert into @test values(123,'ramana')
select * from @test



QUERY:
SELECT S.* FROM(SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 0))AS RID,ID,NAME FROM @TEST)S WHERE S.RID=10

THANKS FOR VISITING BLOG :)

No comments:

Post a Comment