Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Simple ABAP?

Former Member
0 Likes
1,728

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

19 REPLIES 19
Read only

Former Member
0 Likes
1,684

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?

Read only

0 Likes
1,684

for all TRFGR field I need 1st BEGDA and last ENDDA.. can u gve code

Read only

Former Member
0 Likes
1,684

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.

Read only

Former Member
0 Likes
1,684

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

Read only

Former Member
0 Likes
1,684

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.

Read only

Former Member
0 Likes
1,684

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.

Read only

Former Member
0 Likes
1,684

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

Read only

0 Likes
1,684

loop at itab.
  at first.
    write: / itab-TRFGB, itab-ENDDA, itab-BEGDA.
  endat.

  at last.
    write: / itab-TRFGB, itab-ENDDA, itab-BEGDA.
  endat.
endloop.
Read only

jayanthi_jayaraman
Active Contributor
0 Likes
1,684

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.

Read only

Former Member
0 Likes
1,684

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

Read only

0 Likes
1,684

HI Arun,

I tried as u said but when I use at new and at end of..

the date field is displayed as *******.

Read only

0 Likes
1,684

Hi,

Did you tried my suggestion?

Read only

0 Likes
1,684

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

Read only

0 Likes
1,684

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...

Read only

Former Member
0 Likes
1,684

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.

Read only

Former Member
0 Likes
1,684

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.

Read only

Former Member
0 Likes
1,684

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.

Read only

Former Member
0 Likes
1,684

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.

Read only

Former Member
0 Likes
1,684

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.