1 Record From Each Group

  • Hi,

    I have the following table:-

    NameType

    Dog Pet

    Cat Pet

    Monkey Pet

    John Owner

    Jack Owner

    Jim Owner

    I am looking for a query where only 1 Name from each type comes. Example: doing a select on the table and only Dog & Jim come back as a result or Cat & Jack.

    Just 1 row from each Type. Any ideas?

    Bob

  • This should work. It will always return

    Cat Pet

    Jack Owner

    SELECT Min(Name), Type FROM tbl GROUP BY Type

    You can use Max and that'll return John and Monkey

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

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

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