• dwain.c (10/30/2012)


    In the future, you should provide DDL and consumable sample data in the format I've included below.

    -- DDL

    DECLARE @T TABLE

    (RecordID INT, ItemID INT, MaxOfCost MONEY)

    -- Consumable sample data

    INSERT INTO @T

    SELECT 43392,3880,$50.00

    UNION ALL SELECT 39056,3881,$93.75

    UNION ALL SELECT 33941,3881,$97.50

    UNION ALL SELECT 33970,3881,$97.50

    UNION ALL SELECT 38950,3881,$98.75

    UNION ALL SELECT 40417,3881,$96.56

    UNION ALL SELECT 36491,3882,$30.00

    UNION ALL SELECT 37265,3883,$6.50

    UNION ALL SELECT 35054,3883,$4.50

    UNION ALL SELECT 33658,3884,$14.94

    UNION ALL SELECT 36585,3884,$18.77

    UNION ALL SELECT 33134,3884,$14.75

    ;WITH MyData AS (

    SELECT RecordID, ItemID, MaxOfCost

    ,n=ROW_NUMBER() OVER (PARTITION BY ItemID ORDER BY MaxOfCost DESC)

    FROM @T)

    SELECT RecordID, ItemID, MaxOfCost

    FROM MyData

    WHERE n = 1

    Hopefully this provides the results you seek but let us know.

    Thats was quick dwain, and a nice solution though it will suppress any rows that have the same MAX(cost) as another row.

    I suppose the question outstanding is what if there are two rows with the same ItemId and Cost, do you want all rows returned or just one, if just one which one takes takes precedence?

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices