If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Sql Query for top 15 records, 1 per category

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-15-11, 16:30
aish1108 aish1108 is offline
Registered User
 
Join Date: Aug 2007
Posts: 15
Sql Query for top 15 records, 1 per category

Hi,

Here is my current sql which outputs top 15 most emailed items. I am using SQL SERVER 2008, the sql will be in a stored procedure.
Code:
SELECT  TOP 15
		PS.poem_id, 
		PS.vote_average,  
		PS.vote_count, 
		P.poem_title, 
		P.name,
		P.author_id,
		P.bio,
		P.comments, 
		P.poem,
		C.main_category,
		C.subcategories,
		C.h1,
		C.url 
	FROM poems AS P INNER JOIN 
			poem_stats AS PS ON PS.poem_id = P.poem_id INNER JOIN 
				category AS C ON P.category_fk = C.category_id  
	WHERE P.publish_ind='Y' 
	ORDER BY 
		PS.emailed DESC, 
		PS.vote_average DESC
I want instead to output the top 1 most emailed item from each of the 15 main categories. In other words I want each category represented. How do I do this?

Here is the sql to output the main categories
Code:
SELECT DISTINCT main_category FROM category
Thanks a lot!

Last edited by aish1108; 02-16-11 at 10:51. Reason: changed IP to P
Reply With Quote
  #2 (permalink)  
Old 02-16-11, 02:48
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,279
Try
Code:
with CTE(poem_id, vote_average, vote_count, poem_title, name, 
	author_id, bio, comments, poem, main_category,
	subcategories, h1, url, RowNum)
AS 
(	SELECT  
		PS.poem_id, 
		PS.vote_average,  
		PS.vote_count, 
		P.poem_title, 
		P.name,
		P.author_id,
		P.bio,
		P.comments, 
		P.poem,
		C.main_category,
		C.subcategories,
		C.h1,
		C.url,
		ROW_NUMBER() OVER (PARTITION BY C.main_category ORDER BY C.main_category, PS.poem_id) AS RowNum
	FROM poems AS P 
		INNER JOIN poem_stats AS PS ON 
			PS.poem_id = P.poem_id 
		INNER JOIN category AS C ON 
			IP.category_fk = C.category_id  
	WHERE P.publish_ind='Y' 
)

SELECT *
FROM CTE
WHERE RowNum = 1
ORDER BY main_category DESC
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
  #3 (permalink)  
Old 02-16-11, 14:55
aish1108 aish1108 is offline
Registered User
 
Join Date: Aug 2007
Posts: 15
That does the trick. Thanks. I've never used PARTITION before.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On