Solved

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

Posted on 2009-04-06
2
178 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
2 Comments
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 500 total points
Comment Utility
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
Comment Utility
Thank you for your quick, accurate response!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now