• p_shaw (7/17/2014)


    I have a Contract table with a CoverageCode column. I what pull 5 records for each CoverageCode. Currently I am doing this:

    SELECT TOP 5 * FROM Contract WHERE CoverageCode = 1

    UNION

    SELECT TOP 5 * FROM Contract WHERE CoverageCode = 2

    etc.....there are over 100 if I go this route

    Is there a better way to do this?

    Suggestion, do something like this

    😎

    WITH PRE_SEL AS

    (

    SELECT

    ROW_NUMBER() OVER (PARTITION BY CoverageCode ORDER BY (SELECT NULL)) AS PRT_RID

    ,*

    FROM Contract

    )

    SELECT * FROM PRE_SEL WHERE PRT_RID <=5;