Returning one Row per Group

  • Hi,

    I have the following scenario

    CREATE TABLE #Product

    (ProductID smallint,

    ProductName varchar (50),

    ProductCode smallint

    )

    INSERT INTO #Product (ProductID, ProductName, ProductCode)

    (SELECT1 , 'Cheese', 5

    UNION ALL SELECT 1, 'Cheese', 6

    UNION ALL SELECT 2, 'Milk', 4

    UNION ALL SELECT 2, 'Milk', 7

    )

    I need to return a one row per product with either of the codes.

    e.g Query Result should return

    ProductID ProductName Code

    1 Cheese 5

    2 Milk 4

    Any help is appreciated!

  • IMO you'll have to simply order it all

    select ProductID ,

    min(ProductName) as ProductName,

    min(ProductCode) as ProductCode

    from #Product

    group by ProductID

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • Thanks AlzDBA this is exactly what I needed.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply