Pankaj067 (4/16/2013)
Hello cooljagadeesh,You can implement RANK in your query like this
select *,
RANK() over (partition by name order by row) as rank
from @table
also for your filter purpose, the query is
select * from
(
select *,RANK() over (partition by name order by row) as rank from @table
) temp where rank =3
Thanks....:-)
Did you test it against the requirements?
declare @table table(id int ,name varchar(20),row int identity(1,1) )
insert into @table select 1,'a' union all
select 2,'a' union all
select 3,'a' union all
select 1,'b' union all
select 1,'b' union all
select 2,'b' union all
select 3,'b' union all
select 3,'c' union all
select 3,'c' union all
select 3,'c' union all
select 6,'d' union all
select 6,'d' union all
select 7,'d' union all
select 9,'e' union all
select 10,'e' union all
select 11,'e'
-------------------------------------------------
SELECT *,
rk = DENSE_RANK() over (partition by name order by ID)
FROM @table
--also for your filter purpose, the query is
SELECT *
FROM
(
SELECT *,
rk = DENSE_RANK() OVER (PARTITION BY name ORDER BY ID)
FROM @table
) temp
WHERE rk =3
๐
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden