‎2006 Jul 10 11:40 AM
Hi,
How do you select the latest record from a databse table?
Regards
Nishant
‎2006 Jul 10 11:42 AM
‎2006 Jul 10 11:43 AM
sort itab by field1 desending.
1st record is ur latest record.
ex--
sort itab1 by docno decending.
read table itab1 index 1.
‎2006 Jul 10 11:43 AM
‎2023 May 19 8:47 PM
‎2006 Jul 10 11:43 AM
Unless you have a DATE and TIME field in the database table, there is no way of knowing which is the latest record. If you have those fields select all the fields that match your criteria, sort them in DESC manner of DATE and TIME and READ the first row.
Regards,
Ravi
‎2006 Jul 10 11:45 AM
Select the record from the database table in the internal table.
Sort internal table.
READ TABLE itab INTO wa_itab INDEX 1.
You will get the latest record.
Message was edited by: mukesh kumar
‎2006 Jul 10 12:01 PM
Hi Nishant,
You can use the option <b>ORDER BY </b> in your SELECT clause.
This is the SAP Documentation for <b>ORDER BY </b> option.
<i>Variant 1</i>
<b>...ORDER BY PRIMARY KEY</b>
<b>Effect</b>
Sorts the selected lines in ascending order by the primary key of the database table. This variant is only permitted for SELECT * ....
<i>Notes</i>
Since views do not have a primary key, specifying ORDER BY PRIMARY KEY only makes sense with database tables. If, however, you do specify ORDER BY PRIMARY KEY with a view, all fields of the view are sorted in ascending order.
<i>Variant 2</i>
<b>ORDER BY f1 ... fn</b>
<i>Effect</i>
Sorts the selected records in ascending order by the specified column references f1 ... fn. If a list is also specified in the SELECT clause, the column references f1, ..., fn must appear in this list.
By supplementing the statement with DESCENDING, you can sort in descending order using any of the fields f1, ..., fn.
The default sort sequence is ascending order, but you can make this explicit by adding the addition ASCENDING.
Consider this code this will select the latest 10 records form DB table(Note can use <b>Variant 2</b> for DB tables also.
REPORT zarun_1.
DATA : it_mara TYPE STANDARD TABLE OF mara WITH HEADER LINE.
START-OF-SELECTION.
SELECT * FROM mara
INTO TABLE it_mara
<b>UP TO 10 ROWS "No of rows you need(give 1 here)
ORDER BY matnr DESCENDING. "Specify the Key Fields here.</b>
LOOP AT it_mara.
WRITE : / it_mara-matnr,
it_mara-mtart.
ENDLOOP.
Regards,
Arun Sambargi.
Message was edited by: Arun Sambargi
‎2006 Jul 10 12:23 PM
Hi,
As far as I know there is no way you can select latest records from a table in SAP unless date & time field are specified. Transactional data doesn't have date & time fields unlike master data.
Try with Native SQL there may be some options from database end. with in SAP I don't think it is possible but try this option come from Master record for example if you want latest record in EKPO try to come from EKKO as there you have date field.
Regards,
Karthik.k
‎2006 Jul 10 1:46 PM