‎2006 Dec 07 7:33 AM
Hi friends.
I have an interbal table and I wnt to read the first and the last recod from the table based on the entries.
for eg.
TRFGB ENDDA BEGDA
4A 20040131 20030201
4A 20050131 20040201
4A 20050504 20050201
4A 99991231 20050505
ZB 20020131 20011231
ZB 20020615 20020201
ZB 20030131 20020616
Now my output should be..
TRFGB ENDDA BEGDA
4A 99991231 20030201
ZB 20030131 20011231
I cant use 2nd table.
Please help me with a code.....
thanxx in advance
‎2006 Dec 07 7:35 AM
hi
for all the records u need the first and last record? what exactly u need? why dont u choose the record with some condition?
‎2006 Dec 07 7:39 AM
for all TRFGR field I need 1st BEGDA and last ENDDA.. can u gve code
‎2006 Dec 07 7:44 AM
Hi abhay,
1. If TRFGB is your first field of internal table then,
2.
SORT ITAB by TRFGB ENDDA BEGDA.
LOOP AT ITB.
<b> ON-CHANGE OF ITAB-TRFGB.
NEWITAB = ITAB.
APPEND NEWITAB.
ENDON.</b>
ENDLOOP.
regards,
amit m.
‎2006 Dec 07 7:44 AM
Hi Abhay,
Use the following sample code
DESCRIBE TABLE itab LINES len.
IF len GT 0.
* For First record
Read table itab into wa index 1.
* For last record
Read table itab into wa index len.
ENDIF.
Regards
Bhupal Reddy
‎2006 Dec 07 7:44 AM
Abhay,
Declare variable
"data : var type i.
Describe table itab lines var."
Now you will get the total no. of records in your internal table.
decalre internal table itab1 which is having same structure like itab.
read table itab where index = 1.
append lines of itab to itab1.
" Here you will get your column names like TRFGB ENDDA BEGDA
read table itab where index = 2.
append lines of itab to itab1.
" Here you will get first data row like 4A 99991231 20030201.
read table itab where index = var.
append lines of itab to itab1.
" Here you will get first data row like ZB 20030131 20011231
Pls. reward points if helpful.
‎2006 Dec 07 7:51 AM
see the following code, change the field names & table names as you require.
instead of pernr u can use TRFGB
data: begin of itab occurs 0,
pernr like pa0000-pernr,
endda like pa0000-endda,
begda like pa0000-begda,
end of itab.
parameters: p_pernr like pa0000-pernr.
select pernr max( endda ) min( begda )
from pa0000 into table itab
where pernr = p_pernr
group by pernr.
loop at itab.
write:/ itab-pernr, itab-endda, itab-begda.
endloop.
‎2006 Dec 07 7:54 AM
u may want to do a f1 help on the keywords
"at first" and "at last".. which are commonly used to process data within internal table loops.
regards
Charles
abap newbie
‎2006 Dec 07 7:57 AM
loop at itab.
at first.
write: / itab-TRFGB, itab-ENDDA, itab-BEGDA.
endat.
at last.
write: / itab-TRFGB, itab-ENDDA, itab-BEGDA.
endat.
endloop.
‎2006 Dec 07 7:56 AM
Hi,
Here itab is the second tble which should be output.
sort itab1 by trfgb begda.
loop at itab1 into wa1.
*Preparing endda for second table
wa-endda = wa1-endda.
*CHecking for highest endda
loop at itab1 into wa2 where trfgb = wa1-trfgb and endda > wa1-endda.
if wa2-endda > wa-endda.
wa-endda = wa2-endda.
endif.
endloop.
Getting other fields for second table
wa-trfda = wa1-trfda.
wa-begda = wa1-bedga.
append wa to itab.
endloop.
loop at itab into wa.
write : / wa.
endloop.
Hope this helps.If so,kindly reward points by clicking the star on the left of reply.
‎2006 Dec 07 8:16 AM
Hi Abhay ,
Since you dont want to create a new internal table , i.e. you just want to print the data from your internal table .
here is a code which worked for me
tYPES : Begin of ty_1 ,
str type string ,
begda type datum ,
endda type datum ,
end of ty_1.
Data : it_1 type table of ty_1 ,
wa_1 type ty_1,
WA_2 TYPE TY_1.
start-of-selection.
wa_1-str = 'ABC'.
wa_1-begda = '20061101'.
wa_1-endda = '20061102'.
append wa_1 to it_1.
wa_1-str = 'ABC'.
wa_1-begda = '20061103'.
wa_1-endda = '20061104'.
append wa_1 to it_1.
wa_1-str = 'ABC'.
wa_1-begda = '20061105'.
wa_1-endda = '20061106'.
append wa_1 to it_1.
wa_1-str = 'DEF'.
wa_1-begda = '20061101'.
wa_1-endda = '20061102'.
append wa_1 to it_1.
wa_1-str = 'DEF'.
wa_1-begda = '20061105'.
wa_1-endda = '20061106'.
append wa_1 to it_1.
wa_1-str = 'DEF'.
wa_1-begda = '20061103'.
wa_1-endda = '20061104'.
append wa_1 to it_1.
SORT IT_1 BY STR BEGDA ENDDA.
LOOP AT IT_1 INTO WA_1.
WA_2 = WA_1.
* At new STR , prnit the STR and the smallest date
AT NEW STR.
WRITE:/ WA_1-STR , WA_2-BEGDA.
ENDAT.
* At eend of that STR print the largest date
AT END OF STR.
WRITE WA_2-ENDDA.
ENDAT.
ENDLOOP.
Hope this helps.
Regards
Arun
‎2006 Dec 07 8:36 AM
HI Arun,
I tried as u said but when I use at new and at end of..
the date field is displayed as *******.
‎2006 Dec 07 8:40 AM
‎2006 Dec 07 8:42 AM
Hi Abhay ,
If you look at my code the next line after loop at .... is
<b>WA_2 = WA_1.</b>
. This is required because whenyou use At..... , the all fields after the field mentioned in the AT command get converted to ***.
So please add one more line , in which you move the content of your header/workare to another one and the use this for printing.
In case you still have problems please paste your code for this section and i shall try to figure out what is the problem
Regards
Arun
Please reward with point if ans is helpful
‎2006 Dec 07 8:51 AM
Hi,
I'm not sure what you want actually.
but, if u use at new and at end.
the way you position the fields in your itab is important...
using at new (field1)... it checks for the current row in the itab and if the value in field1 in the current row is different from the value of field1 in previous row... it process.. else skip process..
those field2, field3...etc in your itab would be displayed as *****...
there are 2 ways to solve this:
1) position your field1 in the last row of your itab
2) use flag technique...
eg.
data: flag.
loop at itab.
at new field1.
write: / itab-field1.
flag = 'X'.
endat.
check flag = 'X'.
write: itab-field2, itab-field3.....
endloop.
& remember to sort your itab accordingly...
‎2006 Dec 07 8:45 AM
Hi Abhay
i understand you want to display the entries on change of the first field.
Plz try the following code..
after sorting the internal table ;
Data : wa_itab like line of itab,
lv_index type i ,
lv_index_mod type i,
lv_count type i value 0 , " Zero / initial value
lv_lines type i.
Sort itab by TRFGB .
DESCRIBE TABLE itab LINES lv_lines.
lv_index-mod = 1 .
Loop at itab into wa_itab.
lv_index = sy-tabix.
Check lv_index GT 1.
AT END OF TRFGB.
READ TABLE itab into wa_itab INDEX lv_index_mod
Transporting BEGDA.
ADD 1 to lv_count.
MODIFY itab from wa_itab INDEX lv_count.
IF lv_index LT lv_lines.
lv_index_mod = lv_index + 1.
ENDIF.
ENDAT.
ENDLOOP.
ADD 1 to lv_count.
DELETE itab from lv_count.
Loop at itab into wa_itab.
WRITE 😕 wa_itab-TRFGB , wa_itab-ENDDA , wa_itab-BEGDA.
Endloop.
i hope the above code snippet helps you .
if u still face some issues plz revert.
Regards
Pankaj
Note : Plz award points for helpfull answers.
‎2006 Dec 07 9:01 AM
hi,
first u sort the table by ascending.
sort table ascending by TRFGB BEGDA ENDDA.
read table itab index 1 into wa.
assign the required fields to variable.
next sort table descending by TRFGB ENDDA BEGDA
and do the same steps.
‎2006 Dec 07 9:04 AM
Hello Abhay,
Since you need the latest entry both by ENDDA and BEGDA, you can try the following code.
SORT <internal table> BY trfgb endda DESCENDING begda DESCENDING.
DELETE ADJACENT DUPLICATES FROM <internal table> COMPARING trfgb.
The SORT statement is to sort the table in descending order of both endda and begda. The delete statements deletes the unnecessary values of trfgb, except the latest entry.
Hope this helps you,
Regards,
Vijaya.
‎2006 Dec 07 9:30 AM
Hi ,
I have given an example ,hope it helps you .
****************************************************************************
tables mara.
data : it_mara type TABLE OF makt, "internal table of type makt
wt_mara type makt . "work area of type makt
selects all records for the field spras
form makt such that matnr is less than 88
select spras
from makt into corresponding fields of table it_mara
where matnr le '000000000000000088'.
loop at it_mara into wt_mara .
writes only if it is the first record of the internal table .
if sy-tabix EQ 1. " sy-tabix is the sys field ,which has the index of internal table
write 😕 wt_mara-spras.
ENDIF.
endloop.
****do not clear the contents of work area here because the work area contains the last *****record processed but usually it is suggested to clear the work area .
WRITE 😕 wt_mara-spras.
‎2006 Dec 07 12:13 PM
try this
loop at itab into wa.
AT FIRST.
write: / wa-TRFGB,
10 wa-ENDDA,
30 wa-BEGDA.
AT LAST.
write: / wa-TRFGB,
10 wa-ENDDA,
30 wa-BEGDA.
Endloop.