Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Ranking Query Expand / Collapse
Author
Message
Posted Monday, April 15, 2013 4:53 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 10:08 PM
Points: 30, Visits: 158
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
Post #1442536
Posted Monday, April 15, 2013 5:33 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, April 16, 2014 8:36 PM
Points: 364, Visits: 381
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
Post #1442545
Posted Tuesday, April 16, 2013 12:05 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Yesterday @ 1:20 PM
Points: 413, Visits: 443
Select Name,COUNT(distinct id)
from @table
group by name
having COUNT(distinct id) =3
Post #1442599
Posted Tuesday, April 16, 2013 1:50 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 18, 2013 12:37 AM
Points: 33, 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....
Post #1442632
Posted Tuesday, April 16, 2013 2:02 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, April 17, 2014 9:33 AM
Points: 6,754, Visits: 12,854
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
Post #1442635
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse