?
Solved

Need to return only x # of rows per distinct entries in another table

Posted on 2009-04-06
2
Medium Priority
?
185 Views
Last Modified: 2012-05-06
I need to get a result set showing COUNSELOR_RESPONSI.COUNSELOR_TITLE and the top three COUNT(CANDIDATE.CUR_PROG) per each COUNSELOR_TITLE.  I've been able to build the total result set, just not the top 3 by COUNSELOR_TITLE.

Desired result would be like:
Name          Program     Count
counselor1    cur_prog1   3
counselor1    cur_prog2       2
counselor1    cur_prog3       1
counselor2    cur_prog1   10
counselor2    cur_prog2   8
counselor2    cur_prog3   4


Hopefully that makes sense to someone.
SELECT     COUNT(CANDIDATE.CUR_PROG) AS Count, CANDIDATE.CUR_PROG, COUNSELOR_RESPONSI.COUNSELOR_TITLE
FROM         CANDIDATE INNER JOIN
                      NAME_MASTER ON CANDIDATE.HIGH_SCHOOL = NAME_MASTER.ID_NUM INNER JOIN
                      AD_ORGANIZ_MST_EXT ON NAME_MASTER.ID_NUM = AD_ORGANIZ_MST_EXT.ID_NUM INNER JOIN
                      COUNSELOR_RESPONSI ON AD_ORGANIZ_MST_EXT.COUNSELOR_RESP_INI = COUNSELOR_RESPONSI.COUNSELOR_INITIALS
WHERE     (CANDIDATE.CUR_YR = '2008') AND (CANDIDATE.CUR_STAGE = '32')
GROUP BY CANDIDATE.CUR_PROG, CANDIDATE.CUR_YR, COUNSELOR_RESPONSI.COUNSELOR_TITLE
ORDER BY COUNSELOR_RESPONSI.COUNSELOR_TITLE, Count DESC

Open in new window

0
Comment
Question by:RankenIS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 2000 total points
ID: 24077705
This should help you out:
SELECT COUNSELOR_TITLE,CUR_PROG, [Count]
FROM (
SELECT COUNSELOR_TITLE,CUR_PROG, [Count], ROW_NUMBER() OVER ( PARTITION BY COUNSELOR_TITLE ORDER BY [Count] DESC) AS RNUM
FROM (
SELECT COUNSELOR_RESPONSI.COUNSELOR_TITLE, COUNT(CANDIDATE.CUR_PROG) AS [Count], CANDIDATE.CUR_PROG
FROM         CANDIDATE INNER JOIN
                      NAME_MASTER ON CANDIDATE.HIGH_SCHOOL = NAME_MASTER.ID_NUM INNER JOIN
                      AD_ORGANIZ_MST_EXT ON NAME_MASTER.ID_NUM = AD_ORGANIZ_MST_EXT.ID_NUM INNER JOIN
                      COUNSELOR_RESPONSI ON AD_ORGANIZ_MST_EXT.COUNSELOR_RESP_INI = COUNSELOR_RESPONSI.COUNSELOR_INITIALS
WHERE     (CANDIDATE.CUR_YR = '2008') AND (CANDIDATE.CUR_STAGE = '32')
GROUP BY CANDIDATE.CUR_PROG, CANDIDATE.CUR_YR, COUNSELOR_RESPONSI.COUNSELOR_TITLE
) AS TEMP ) AS TEMP1
WHERE RNUM <= 3

Open in new window

0
 

Author Closing Comment

by:RankenIS
ID: 31567039
Thank you for your quick, accurate response!
0

Featured Post

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A recent questions about how to add SSRS named instances, couldn't find any that talks about SQL server 2008, anyway I decided to help by creating some screen shots. The installation is straightforward, you just pop the SQL server 2008 installati…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question