Simple Query Question - How do I return the Last 3 records of a Table?
Question.
For example, I have a table that has 50 records.
How do I, specify in SQL to only return the last 3 records of the table.
Select a.* from table a where ????
I was just trying to show an example to a friend on
how something like this would work and if it was even possible. But it won't work. Here's a simple example:
SQL> create table emp
2 (id)
3 as
4 select object_id
5 from all_objects
6 order by object_id;
Table created.
SQL> select *
2 from (select rownum rn
3 ,b.*
4 from emp b)
5 where rn > ( select (max(rownum) - 3)
6 from emp)
7 ;
RN ID
40830 55891
40831 55892
40832 55893So far, so good. These are the "last 3" rows inserted. Now delete a bunch of rows and insert 3 new ones:
SQL> delete emp where id < 40000;
33423 rows deleted.
SQL> commit;
Commit complete.
SQL> insert into emp values (60000);
1 row created.
SQL> insert into emp values (60001);
1 row created.
SQL> insert into emp values (60002);
1 row created.
SQL> commit;
Commit complete.
SQL> select *
2 from (select rownum rn
3 ,b.*
4 from emp b)
5 where rn > ( select (max(rownum) - 3)
6 from emp)
7 ;
RN ID
7410 55891
7411 55892
7412 55893Here's the problem. Even though the "last 3 rows" are 60000 - 60002, I still get the same ones as the first query.
Similar Messages
-
How to get to the last record of an APEX report automatically?
In Oracle Forms there is a "lastrec" command which can be placed after a call to a multi-row form. The records called have the last record at the bottom of the page. For example, if there are 100 records and there are 10 rows on the form, record numbers 91 through 100 would be showing. Can this be done in APEX? I have a user who wants it this way. I set the report page up to be ordered descending, which gives her last record at the top of the page, but this is not acceptable to her. She wants to see the last record, but she wants it at the bottom of the page.
Hi,
Try this query, it should order your data as you want and the report pagination is not broken!
The number of row displayed must be the same as the divide by value (ie. 10)
select your_id
from your_table
order by trunc ((DENSE_RANK() OVER ( ORDER BY your_id desc) -1) /10)
, DENSE_RANK() OVER ( ORDER BY your_id desc) desc;
Page 1 displays 91 to 100
Page 2 displays 81 to 90
Also if your data set have 87 rows, Page 1 will display 78 to 87 (ie. the 10 last rows).
If you want more details regarding the sorting you can have a look on this query:
select your_id
, trunc ((DENSE_RANK() OVER ( ORDER BY your_id desc) -1) /10)
, DENSE_RANK() OVER ( ORDER BY your_id desc)
from your_table
order by 2, 3 desc; -
Why my vector() always always return the last record?
Whenever i uses Vector, my whole vector always return the last element.. why?? Anyway to solve this?
some examples of my codes..
(String)rowVector.elementAt(2)
My rowVector vector always return the last element..
while(rs1.next())
Vector rowVector = new Vector();
for (int currentCol = 0; currentCol < numCols; currentCol++)
rowVector.add(rs1.getString (currentCol + 1));
tableData.addElement(rowVector);That means that every element in the Vector refers to the same object. I am surprised that your JDBC driver does not return different string objects for each call, but to work around that, try this:
rowVector.add(new String(rs1.getString (currentCol + 1))); -
How to get the last record??
Hi, I now have to get only the last record from one table
from MS Access.
I was looking for the useful method from API, however,
I cannot get it.
How to get only the last record from one table ???
Please help, thanks.In RDBMS, row order is really not relavent by iteself. Meaning there is no such thing as 'last record' unless it's in the context of a column (e.g. timeStamp).
Of course, rows are inserted and stored in some natural order, but you cannot assume they'll come back the same way in a 'Select *' as they were entered.
So, if you mean last row as in last inserted row, I would add a timestamp field that's set at insert, or use a autonumber column. Either way, have a column that will always contain the highest number or newest timestamp, and then you can build your where clause from that.
For instance, in Access, I have an autonumbered field called 'fred', with other columns. If I want the last record, I simply use the following:
select * from atable where fred in (SELECT max(fred) AS Max FROM atable);
the 'where in' clause will filter to only those records in the subsequent select statment, which of course is only one, the max of the column fred.
bRi -
Updating only the last record.
Hi,
I have a scenario where we get more than 1 record based on style. Everytime the new record comes in, I need to update the last record inserted. It requires a procedure to do it, but need some help on how to just update the last record only and not all the previous received records.
Here is the scenario.
create table test_1 as
with data_Set as
( select 'ABCD' style, 20080101 date_received, 2 duration,20080301 expire_Date from dual union all
select 'PQRS' style, 20080201 date_received, 2, 20080401 expire_Date from dual
) select * from data_set
so now on next run when i get another record
insert into test_1
select 'ABCD' style, 20080401 date_received, 3 duration,20080701 expire_Date from dual.
now the procedure should insert the incoming record and also update the expire_date for the previous record to a day before date_received for the new record. I am ok doing it when its only 2 records , but when i get another record of same style, i dont want to update all the previous received records. I only want to update the last record on file.
can anyone suggest?STYLE DATE_RECEIVED DURATION EXPIRE_DATE SEQ_INSERTED
110427 7/31/2006 0 7/31/2006 2
110427 9/1/2007 12 9/1/2008 2
110427 8/2/2008 24 8/2/2010 3
468130 3/13/1997 0 2
468130 3/13/1997 12 3/13/1998 2
468130 1/12/2008 12 1/12/2009 3
Here is my data set
and here is the query i use to update, does it look correct?
update test b
set ( expire_Date) =
(select start_date - 1
from test a
where (style_no, date_received) in
(select style_no, max(date_received)
from test c
group by style_no
and a.style_no = b.style_no
and style_no in ('468130','110427') -
Read the last record in the database table..
Hi..
I am new to ABAP. So i have one condition to read the address number field in the last record from database table by using the person number where clause field.
But the person number is same for some the records.
So can any tell me the logic for to read the addressnumber field in the last record of selected records from database where personnumber is equal to some thing.
Please help me..
Thanks
venkiHi Venkat ,
u mean to say
person number addressnumber
aa1 0000000000001
aa1 0000000000002
aa1 0000000000003
aa1 0000000000004
aa2 0000000000021
aa3 0000000000110
if i have to read the addressnumber 00000000000004
of the person number aa1
u can go like
data v_address number like addressnumber.
sort itab by personnumber addressnumber
loop at itab .
at end of personnumber.
move addressnumber to v_addressnumber ..
endat.
endloop.
so using control break statements i can fetch the last record of my person number if at all the same person number is having more than one address number ..
hope this helps,
Regards,
Vijay
Message was edited by: Vijay -
How do you return the number of Rows in a ResultSet??
How do you return the number of Rows in a ResultSet? It's easy enough to do in the SQL query using COUNT(*) but surely JDBC provides a method to return the number of rows.
The ResultSetMetaData interface provides a method for counting the number of columns but nothing for the rows.
ThanksNo good way before JDBC2.0. u can use JDBC2.0 CachedRowSet.size() to retrieve the number of rows got by a ResultSet.
-
How to return the entire row from a table
Hi guys,
Tabl A
FirstName varchar2(10),
Age Number,
Add varchar2(100),
Table B
Filedname varchar2(200), -- contains all the fields of table A
Datatype varchar2(100), -- Contains the datatypes of the filedname
Length Number -- contains the length of the fileds
Now how can i return the entire row from table A with the datatype and its length.
Any help would be appreciated ?
Here is the query I wrote but it how to take the datatype and its length..
create or replace procedure disp_table_data
as
type r_cursor is REF CURSOR;
c_A r_cursor;
er A%rowtype;
begin
open c_A for select * from A;
loop
fetch c_A into er;
exit when c_A%notfound;
dbms_output.put_line(er.FirstName||'--'||er.Age);
end loop;
close c_A;
END;
/Guys,
I want column value from table A and its corresponding data_type and length from table B. I think I cant do it through joins.
Any idea would be appreciated ?.
Example ..
Tabl A
FirstName varchar2(10),
Age Number,
Add varchar2(100)
insert into A values('John',24,'Boston');
Table B
Filedname varchar2(200), -- contains all the fields of table A
Datatype varchar2(100), -- Contains the datatypes of the filedname
Length Number -- contains the length of the fileds
insert into B values('FirstName','varchar2',10); -- this is coming from table A.
Output should be like this...
John,Varchar2,10
Here (John is the FirstName - coming from table A, Varchar2 is a Datatype and 10 is the Length which are coming from table B ). Only column values are coming from table A , corresponding datatype and length are coming from B.
Any idea would be appreciated ?
Thanks. -
HELP! Last second extra credit (before midnight), how can i return the node
While traversing a tree... How can i return the node I am currently at?
Or perhapspublic Node getCurrentNode() {
return currentNode;
}Your question has a large number of possible interpretations. -
Using Numbers spreadsheet and In working on a simple home budget, how can I change the color of a number to RED when it comes up negative. (only during the time that it's negative) Thanks, ( I hope I did this right!!)
The Inspector offers choices of format for negative numbers when you choose Number of Currency as the format for a cell. Also offered when the format is set to Automatic, IF the selected cell(s) contain number(s).
The choices are in the pop-up menu beside the box for number of decimal places.
Regards,
Barry -
Topic area: Apple Mail. Question: How do I get the message preview window to display to the right of my list of email messages? i.e. to preview whatever message is currently highlighted in the center window list of messages?
At present there is no preference option available to move the preview
pane to right side of the mail window.
Best. -
Simple/silly question: how do I set/change default font/color for outgoing mail messages?
Just a suggestion..........
Download Thunderbird. Easier to use when it comes to what you want to do w/your emails. -
How can I return the current Date in Java?
I am new to Java, and basically I want to know how I can return the current date as a variable, so for example, I have declared the following date variable
private java.util.Date currentDate;How can I return the current date, do I have to write a method? To be more specific, I need to write a setMethod, that simply returns the current Date in Java.
Please point me in the right direction.
Thanks,If all you want is a date for printing or displaying, the easiest way is to use Date as shown below. If you need something more complex, look at the Calendar API.
import java.util.Date;
class DateEx
public static void main(String args[])
Date now = new Date();
System.out.println(now);
} -
TS1702 How can I return the app that I bought for my iPhone4S almost three months ago.
How can I return the app I bought almost 3 months ago? Is this to late to return??
Apps are not returnable. They're also very difficult to wrap.
-
Oracle Forms returns the first record in the database when performing query
Once in a while when we query for a record on a form, say by first name Tom, then it returns the first record in the database. Other times it return the Tom's record. It only happens once in a while and if you close the form and reopen it and requery for Tom, then it brings Tom's record.
Does anyone know the issue what could be happening. It just happens every now and then that it's hard to reproduce.
ORacle Forms 10GR2
ORacle Application Server 10.1.3
thanksthen it returns the first record in the databaseI'm not sure if i understand you correctly. Do you mean forms ignores the searc-condition you entered? I would check SYSTEM.LAST_QUERY at the moment this happens to check if the condition gets somehow lost.
Maybe you are looking for
-
Iphoto won't install on snow leopard
Have an iMac that was running Tiger that required a complete hard drive reformat. Upgraded to Snow Leopard and went to the App Store to purchase iLife (or iPhoto at the very least) and the minimum requirements are 10.7.4 ... I do not want to update p
-
This is a recent problem. Any ideas? Thank you, Kerrytwix
-
Hi.. How do we know which message type is appropriate for our document type.? Can any one give the whole list of message types used in interface. Regards Sampath
-
Can't install Intel Rapid Storage Technology driver on Yoga 2 11
The Lenovo driver list for the Yoga 2 11 includes Intel Rapid Storage Technology (IRST) Driver for 64-bit Windows. However, attempting to install this results in the message, "No Appropriate Driver to be Installed". Likewise, attempting to install th
-
Setting constraint values inside states
Hi all, in the code attached I have a state called 'pods1' - I have a canvas called pod1 when I'm in design view and I change states to 'pods1' it works fine - but if I change to this state programatically it says 'left' can't be found in <mx:canvas>