SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Ranking Query


Ranking Query

Author
Message
cooljagadeesh
cooljagadeesh
SSC-Enthusiastic
SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)

Group: General Forum Members
Points: 152 Visits: 228
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
Mansfield
Mansfield
Say Hey Kid
Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)

Group: General Forum Members
Points: 712 Visits: 394
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 :-)
Anjan@Sql
Anjan@Sql
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1258 Visits: 1498
Select Name,COUNT(distinct id)
from @table
group by name
having COUNT(distinct id) =3
Pankaj067
Pankaj067
Valued Member
Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)

Group: General Forum Members
Points: 55 Visits: 68
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....:-)
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42290 Visits: 20012
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
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search