SQL Query help

  • Hi there

    Could u pls help to achieve this. This is not simple as u guys think pls send me the query instead os telling its simple

    i need top version on each id as a result

    ORIGINAL DATA

    id version text age sex

    1 1 aa 1 M

    1 2 bb 2 M

    1 3 cc 1 M

    2 1 dd 3 M

    3 1 ee 4 M

    3 2 ff 1 F

    RESULT

    ID VERSION text age sex

    1 3 cc 1 M

    2 1 dd 3 M

    3 2 ff 1 F

    Cheers

  • sqlcool (1/26/2009)


    Hi there

    Could u pls help to achieve this.

    i need top version on each id as a result

    ORIGINAL DATA

    id version

    1 1

    1 2

    1 3

    2 1

    3 1

    3 2

    RESULT

    ID VERSION

    1 3

    2 1

    3 2

    Cheers

    Be more than glad to help, once you show us what you have tried so far. Has something to do with fish...

  • Two possible answers, both will work:

    TOP

    ROW_NUMBER

    ----------------------------------------------------The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood... Theodore RooseveltThe Scary DBAAuthor of: SQL Server 2017 Query Performance Tuning, 5th Edition and SQL Server Execution Plans, 3rd EditionProduct Evangelist for Red Gate Software

  • Lynn Pettis (1/26/2009)

    Be more than glad to help, once you show us what you have tried so far. Has something to do with fish...

    Mmm, learning you are... 🙂

    David

    @SQLTentmaker

    “He is no fool who gives what he cannot keep to gain that which he cannot lose” - Jim Elliot

  • hmmm, yes. Simple ones too easy to just give away.

  • Grant Fritchey (1/26/2009)


    Two possible answers, both will work:

    TOP

    ROW_NUMBER

    Simple MAX would do it...

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • Think, actually a GROUP BY with MAX in SELECT, hmmm

  • Jeff Moden (1/26/2009)


    Grant Fritchey (1/26/2009)


    Two possible answers, both will work:

    TOP

    ROW_NUMBER

    Simple MAX would do it...

    Simple isn't nearly as much fun. Small data set, ROW_NUMBER may perform better.

    ----------------------------------------------------The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood... Theodore RooseveltThe Scary DBAAuthor of: SQL Server 2017 Query Performance Tuning, 5th Edition and SQL Server Execution Plans, 3rd EditionProduct Evangelist for Red Gate Software

  • sqlcool (1/26/2009)


    Hi there

    Could u pls help to achieve this.

    i need top version on each id as a result

    ORIGINAL DATA

    id version

    1 1

    1 2

    1 3

    2 1

    3 1

    3 2

    RESULT

    ID VERSION

    1 3

    2 1

    3 2

    Cheers

    select

    from

    group by

    Nothing fancy.

Viewing 9 posts - 1 through 8 (of 8 total)

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