Ranking Query

  • Hi All,

    i have a requirement to find out which employee pass three exams continuously

    if you observe below query dataset

    a passed three exams continuously

    again b also passed

    c and d failed

    again e passed

    finally it should display a,b,e rows only

    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 * from @table

    here i am trying to fetch by using ranking functions but not able to find out the query

    please help any one

    Regards,

    Jagadeesh

  • You'll probably need a query that GROUP BY the name column. Combine this with a HAVING search condition that checks for MIN(), MAX() and COUNT(DISTINCT ) should get you what you want.

    GROUP BY

    HAVING

    Alternatively, you can solve this problem by having two joins to the same table, but joining to the previous and next ID values, respectively.

    Looking forward to see what query you come up with πŸ™‚

  • Select Name,COUNT(distinct id)

    from @table

    group by name

    having COUNT(distinct id) =3

  • 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....:-)

  • 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

    πŸ˜‰

    β€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    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

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply