2

I have the following query. What results is two coloums, oakid and max(count(rating)). What I want is to have two columns, but instead of actually having the max(count(rating)), I want the rating associated with the max(count(rating)). How can I modify my query to give me this?

select oakid, 
  (select max(count(rating)) 
     from climbs, routes 
     where climbs.routename = routes.name 
     and climbs.climberid = oakid group by routes.rating) 
  as skill
 from climbers;

For example, if I have ratings "hard, hard, easy, easy, easy, medium", the max(count(rating)) will show "3", since there are 3 "easy" ratings, but I want it to show "easy", not "3".

2 Answers 2

2

It sounds as though you want the statistical mode (most frequently occurring) rating for each oakid or climberid. Oracle has a handy function to do this for us called STATS_MODE:

SELECT c.climberid AS oakid, STATS_MODE(r.rating) AS skill
FROM climbs c
INNER JOIN routes r ON (c.routename = r.name)
GROUP BY c.climberid;
Sign up to request clarification or add additional context in comments.

1 Comment

The way you rewrote the query doesn't work for my database, but the stats_mode was helpful. Thanks.
0

try the following:

select oakid, 
  (select rating
     from climbs, routes 
     where climbs.routename = routes.name 
     and climbs.climberid = oakid 
         group by routes.rating 
         having max(count(rating)) = count(rating)) 
  as skill
 from climbers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.