How we could fine the last row in a record

  • Hi,

    How to write query in which I need to identify the last the row from the records?

    Thanks,

    SS

  • sheetalsh (6/23/2008)


    Hi,

    How to write query in which I need to identify the last the row from the records?

    Thanks,

    SS

    Rows are not ordered. You can introduce ordering by adding a column that keeps track of the order. You could base it on a timestamp, date (e.g. datetime column with get_date() default), sequence number (see identity columns). Once you have set this up you can get the last entry by using e.g. ORDER BY and TOP.

    Regards,

    Andras


    Andras Belokosztolszki, MCPD, PhD
    GoldenGate Software

  • I think you can use ranking functions and assign a row number for each record, and then you can get the last row.

    SELECTTOP(1) QuestionId,

    ROW_NUMBER() OVER (ORDER BY QuestionId DESC) as RowNumber

    FROMtblQuestions

    or for a more simple situation you can sort the values DESC, and get the TOP (1), so that you get the last record.

    SELECTTOP(1) QuestionId

    FROMtblQuestions

    ORDER BY QuestionId DESC

  • Hi,

    Many thanks for your reply.

    Thats great.

    Even we can do it as follows:

    delete from BTBLPRODUCT_MST where COMP_ID IS NULL AND PRODUCT_ID = (SELECT MAX(PRODUCT_ID) FROM BTBLPRODUCT_MST)

    Am I right??

  • yes, and i think for the delete operation your solution is better.

  • Hi,

    Thanks buddy.

  • Hi,

    use this Query...

    with numbered as(SELECT rowno=row_number()

    over (partition by rand() order by rand())from )

    Delete from numbered where rowno= (select max(rowno) from numbered)

  • with numbered as(SELECT rowno=row_number()

    over (partition by rand() order by rand())from )

    select * from numbered where rowno= (select max(rowno) from numbered)

  • sorry syntax is missed.

    with numbered as(SELECT rowno=row_number()

    over (partition by rand() order by rand())from Tablename)

    Delete from numbered where rowno= (select max(rowno) from numbered)

  • Hi,

    Thanks a lot:)

  • The major point has been missed... Andras explained it. You need a date column or an IDENTITY column to figure out what the last row actually is. Adding an artificial Row_Number() column is a crap shoot at best. There's no guarantee that the last row is the last row you're thinking of...

    --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)

  • Even i am of same opinion as of Jeff's

  • Jeff Moden (6/24/2008)


    The major point has been missed... Andras explained it. You need a date column or an IDENTITY column to figure out what the last row actually is.

    And I would be unwilling to trust an IDENTITY column. (I have been bit by it.) The DB engines are willing, when it meets their needs, to populate your table in an order that does not match the order of your input. It doesn't happen that often, but when it does, the explanation is simply that a table is an unordered set of rows. To be safe, I think you really need some data in your input that can be used to distinguish your definition of "last" row.

  • Jim Russell (6/25/2008)


    Jeff Moden (6/24/2008)


    The major point has been missed... Andras explained it. You need a date column or an IDENTITY column to figure out what the last row actually is.

    And I would be unwilling to trust an IDENTITY column. (I have been bit by it.) The DB engines are willing, when it meets their needs, to populate your table in an order that does not match the order of your input. It doesn't happen that often, but when it does, the explanation is simply that a table is an unordered set of rows. To be safe, I think you really need some data in your input that can be used to distinguish your definition of "last" row.

    You're right during bulk loading ops. BCP and SELECT INTO can under certain rules run inserts on a data file as a PARALLEL data load, in which case you can't guarantee your order unless you already have an inbuilt IDENTITY and you use the KEEPIDENTITY flag.

    If you have the luxury - you could use an ORDER BY in the insert command (which WILL guarantee that the identity column values will be assigned in that order), but that can be a big IF, and not something available in every scenario.

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

Viewing 14 posts - 1 through 13 (of 13 total)

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