• Agreed that not sure why you wouldn't want to use a CTE, but here's another alternative.

    DECLARE @h TABLE

    (House_Acc INT, Accountid INT, repcode VARCHAR(10))

    INSERT INTO @h

    SELECT 123, 1, 'J978A'

    UNION ALL SELECT 123, 2, 'J978A'

    UNION ALL SELECT 123, 3, 'J978A'

    UNION ALL SELECT 123, 4, 'EG567'

    UNION ALL SELECT 456, 21, 'BR5TG'

    UNION ALL SELECT 456, 22, 'BR5TG'

    UNION ALL SELECT 678, 66, 'ZHR06'

    UNION ALL SELECT 678, 45, 'ZHR06'

    UNION ALL SELECT 678, 34, 'NH678'

    SELECT a.House_Acc, b.AccountID, b.repcode

    FROM (

    SELECT House_Acc, AccountID, repcode

    ,m=MAX(repcode) OVER (PARTITION BY House_Acc)

    FROM @h) a

    INNER JOIN @h b

    ON a.House_acc = b.House_acc

    WHERE m <> a.repcode

    Any solution posed with a CTE can always be done without the CTE by making the CTE into a derived table as Lynn has shown.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St