• Kl25, thanks a lot for the idea. Your query gave a slightly different result but i used your idea to generate the required result.

    Basically first find the minimum group id for each item, then find the minimum group id (based on item's min group id) for each group.

    SELECT DISTINCT c.GroupMinGroupId, a.ItemId, a.ItemName

    FROM #Data a

    INNER JOIN

    (

    SELECT MIN(b.ItemMinGroupId) AS GroupMinGroupId, a.GroupId

    FROM #Data a

    INNER JOIN

    (

    Select MIN(GroupId) as ItemMinGroupId, ItemId

    From #Data

    Group by ItemId

    ) b ON a.ItemId = b.ItemId

    GROUP BY a.GroupId

    ) c ON a.GroupId = c.GroupId

    Here is the output.

    GroupMinGroupIdItemIdItemName

    1100Bike

    1200Bottle

    1300Helmet

    4400Bread

    4500Egg

    Thanks Kl25.