How will fetch last 5 rows from table in db2
Answers were Sorted based on User's Feedback
Answer / sree
SELECT * FROM TABLENAME ORDER BY PRIMARYCOLUMNNAME DESC
FETCH FIRST 5 ROWS ONLY ;
THIS ANSWER IS VALID ONLY IF THERE IS A PRIMARY KEY
COLUMN,OTHERWISE IT WONT WORK
IF THERE IS NO PRIMARY KEY COLUMN IN GIVEN TABLE ,I DON'T
KNOW THE ANSWER
FOR FETCHING FIRST 5 ROWS
SELECT * FROM TABLENAME FETCH FIRST 5 ROWS ONLY;
| Is This Answer Correct ? | 8 Yes | 1 No |
Hi Harsha,
There is nothing to do with multiple of 5.
Simply declare the array of occours of 5 keep on filling
it when 6th record comes start over writing it from 1st.
At last we only left with the last 5 fetched rows.
Vaneesh Khurana
| Is This Answer Correct ? | 1 Yes | 0 No |
See there is no direct method to do so, but still if we want
last 5 rows only we can create a logic which is as follows.
1. Declare an array to save the fetch rows with occurs of 5.
2. Now fetch the rows and start saving in that array such
that when the count becomes 5 overwrite the 6th fetch in the
first occurs similarly the 7th fetch on the 2nd occur.
3. Now when all the fetching is done the array will contains
the last 5 fetched rows.
| Is This Answer Correct ? | 2 Yes | 2 No |
Answer / harsha
Vaneeshh, your logic will not work if number of records are
not in a multiple of 5.
| Is This Answer Correct ? | 1 Yes | 1 No |
Answer / mahyn
select * from tablename
order by primary_key desc
fetch first 5 rows only
with ur;
can be one way...
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / mayur bhayani
SELECT * FROM (SELECT * FROM T ORDER BY pk DESC FETCH FIRST 5 ROWS) AS X
ORDER BY PK;
| Is This Answer Correct ? | 3 Yes | 11 No |
I have two tables A and B. Both tables are connected with SSN number as foreign key. I want to retrieve a column data from B table. How will you write a query?
can we drop column from a table
Can we use select * statement in cobol program without giving field names ???
how can you copy records present in a flat physical file to a database file(physical file).please explain in rpg400 with a simple example
What is blu acceleration in db2?
What are bind parameters in db2?
In a Cobol-DB2 program, I am fetching rows from 4 tables using cursor and then based on the a field present in that table, It processes the information accordingly..for example stat-c is one digit field..if stat-c is 'D' then the a row is deleted from table and written those details in to a file. If the stat-c is 'U' then a row is updated (hardcoded what to update)in a table and written those details in to a file. If the stat-c is 'I' then a row is inserted in a table and written those details in to two files. The issue is i have to include the intermediate commits. When an abend occurs, due to commit statement db2 tables will be saved, But there will be lose of file contents. When we resubmitting the job associated with this program there will be insert ,update and delete anomolies to avoid that what measures could be taken?. The intermediate commit is nothing but issuing commit after massive inserts, updates and deletes(sum of 500actions)
how 2 resolve the -311 sqlcode
How do you simulate the explain of an embedded sql statement in spufi/qmf?
How do I optimize a query in db2?
What is the use of dclgen in db2?
Define db2.