Query [message #25914] |
Mon, 26 May 2003 06:04 |
Prabodh
Messages: 2 Registered: May 2003
|
Junior Member |
|
|
I have one Question regarding DBMS, it may be Oracle or SQL Server.
Suppose we have EMP table with columns as EmpID, EName and Salary.
It has records as .......
EmpID EName Salary.
1 ABC 1000
3 XYZ 5000
5 PQR 10000
100 UVW 10000
I want to get the last but one (Second Last) record from the table. How can I do that?
|
|
|
|
Re: Query [message #25921 is a reply to message #25916] |
Mon, 26 May 2003 23:24 |
Prabodh
Messages: 2 Registered: May 2003
|
Junior Member |
|
|
Thanks for your reply.
I have also got one answer, where it is suppposed to be ordered by empID.
select * from Emp where EmpID=
(
select max(EmpID)
from Emp
where EmpID<(select max(EmpID) from Emp)
)
But what if data is not in ordered format?
Can we do something using RowID? if yes How?
Thanks,
Prabodh
|
|
|
sorting data in Oracle: can't use ROWID! [message #25924 is a reply to message #25921] |
Tue, 27 May 2003 01:07 |
|
Maaher
Messages: 7065 Registered: December 2001
|
Senior Member |
|
|
The solution given also works, but is more limited. It only works if you're looking for the second highest empID. But it works. As usual in SQL, there are several ways to accomplish the desired result. You just need to look at performance. What option is the most performant one in YOUR environment?
Now, ROWID is not an option since it is the physical location of a record. You cannot see it as a sequence number. Sometimes you read about people who claim that ROWID is the way to go, and they even might give you an example, but trust me on this one: you cannot rely on ROWID to sort your data. I believe Tom Kyte has explained it (it is about the last record, but you'll catch the drift). If you want to order you need to have something to order your data by. Typically you would create some audit fields (at least we do): creation_date, creation_user, modification_date, modification_user (of course the dates are more important (since they are used to sort our data) than the "user" fields). The audit fields are filled automatically unsing triggers.
MHE
|
|
|