I have a scenario where there are 4 records with same ID. I need to write a code so that i can take only last 3 records

  • I have a scenario where there are 4 records with same ID. I need to write a code so that i can take only last 3 records.

    Can anyone give me any idea what to do?

  • Look for ROW_NUMBER() in BOL (BooksOnLine, the help system usually installed together with SQL Server).

    It requires an column for sorting. So you have to think what would be the definition for "last 3"... 😉



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Modify the following:

    ;with numbered as(SELECT rowno=row_number() over

    (partition by your duplicate column, order by Your duplicate column),col1, col2, col3 from your table name)

    select * from numbered WHERE Rowno > 1

    For more specific assistance please read the article whose link is in my signature block. This will help those who will try to help you, with a tested answer.

    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]

  • Thanks for the replies

    I will try it out

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

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