June 23, 2008 at 1:32 am
Hi,
How to write query in which I need to identify the last the row from the records?
Thanks,
SS
June 23, 2008 at 1:47 am
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
June 23, 2008 at 1:49 am
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
June 23, 2008 at 1:55 am
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??
June 23, 2008 at 2:03 am
yes, and i think for the delete operation your solution is better.
June 23, 2008 at 2:11 am
Hi,
Thanks buddy.
June 23, 2008 at 3:38 am
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)
June 23, 2008 at 3:39 am
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)
June 23, 2008 at 3:42 am
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)
June 23, 2008 at 7:02 am
Hi,
Thanks a lot:)
June 24, 2008 at 5:15 pm
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
Change is inevitable... Change for the better is not.
June 24, 2008 at 5:39 pm
Even i am of same opinion as of Jeff's
June 25, 2008 at 12:34 pm
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.
June 25, 2008 at 1:10 pm
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