Created
October 27, 2011 07:03
-
-
Save keithbloom/1318949 to your computer and use it in GitHub Desktop.
TopN Stored Procedure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT TOP 5 description, hit_count |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE PROCEDURE dbo.media_TopNCategories | |
( | |
@category varchar(300), | |
@TopN int | |
) | |
as | |
CREATE TABLE #top_results ( | |
id [int] IDENTITY(1, 1), | |
description [varchar] (300), | |
hit_count [int] | |
) | |
INSERT #top_results (description, hit_count) | |
SELECT m.description, m.hit_count | |
FROM dbo.media m | |
JOIN dbo.media_categories mc | |
on mc.id = m.category_id | |
WHERE mc.description = @category | |
ORDER BY m.hit_count DESC | |
SELECT id, description, hit_count | |
FROM #top_results | |
WHERE id < = @TopN |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment