Select top 1 with disctinct

  • My table has Order, Release, Line and many other columns. I want to select just the top 1 line for every distinct Order and Release combination. I don't care which line I get because the other columns I want to retrieve are the same for all rows.

    How can I get a distinct list based on these two key fields, but still grab a couple other columns like Ref1 and Ref2.

  • It would assist those who would like to assist you if you would post the table definition, some sampe data and desired results from that sample data. Refer to the first link in my signature block to see how to do this most easily..

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • mpv-1060508 (11/24/2010)


    My table has Order, Release, Line and many other columns. I want to select just the top 1 line for every distinct Order and Release combination. I don't care which line I get because the other columns I want to retrieve are the same for all rows.

    How can I get a distinct list based on these two key fields, but still grab a couple other columns like Ref1 and Ref2.

    Have you tried?:

    SELECT order, release, MAX(Line) AS Line, ... FROM table GROUP BY order, release


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

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

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