Application Development 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: 

urgent problem: grouping by fields of internal table

0 Kudos

Hi friend,

i have a urgent problem and i hope that you can help me out:

i have an internal table containing data, and i sorted it by 3 fields.

now what i want to do is kind of grouping that means that all the lines that are the same should be written into another internal table like this:

A

A

B

B

B

C

D

would give me:

A 1

A 1

B 2

B 2

B 2

C 3

D 4

How could i do this ( especially avoiding nested loops ? ) Is there a handy trick to do this efficiently ?

thank you, i will give points immediately,

Clemens

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Solved?

Regards, Dieter

5 REPLIES 5

Former Member
0 Kudos

Hello,

The field position is important for the fields that are using in the control break statements..Also SORT internal table is required.

http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb381a358411d1829f0000e829fbfe/content.htm

DATA: BEGIN OF ITAB OCCURS 0,
MATNR TYPE MATNR,
WERKS TYPE WERKS_D,
VALUE TYPE NETPR,
END OF ITAB.
 
ITAB-MATNR = 'ABC'.
ITAB-WERKS = '0100'.
ITAB-VALUE = '10.00'.
APPEND ITAB.
 
ITAB-MATNR = '1ABC'.
ITAB-WERKS = '0100'.
ITAB-VALUE = '10.00'.
APPEND ITAB.
 
SORT ITAB BY MATNR WERKS.
 
LOOP AT ITAB.
 
 AT FIRST.
  WRITE : 'AT FIRST'.
 ENDAT.
 
 AT NEW MATNR.
  WRITE : 'AT NEW MATERIAL NUMBER'.
 ENDAT.
 
 AT END OF MATNR.
  SUM.
  WRITE: / ITAB-MATNR, 'MATERIAL TOTAL - ', ITAB-VALUE.
 ENDAT.
 
 AT END OF WERKS.
  SUM.
  WRITE: / ITAB-WERKS, 'PLANT TOTAL - ', ITAB-VALUE.
 ENDAT.
 
 AT LAST.
  WRITE : 'AT LAST'.
 ENDAT.
ENDLOOP.

Regards,

Deepu.K

Former Member
0 Kudos

Use collect

Former Member
0 Kudos

Hi clemens,

here a short example:

DATA: BEGIN OF ITAB OCCURS 0,

TXT(1),

END OF ITAB.

DATA: BEGIN OF ITAB1 OCCURS 0,

TXT(1),

N(1) TYPE N,

END OF ITAB1.

*

ITAB-TXT = 'A'. APPEND ITAB.

ITAB-TXT = 'A'. APPEND ITAB.

ITAB-TXT = 'B'. APPEND ITAB.

ITAB-TXT = 'B'. APPEND ITAB.

ITAB-TXT = 'B'. APPEND ITAB.

ITAB-TXT = 'C'. APPEND ITAB.

ITAB-TXT = 'D'. APPEND ITAB.

*

DATA: I(1) TYPE N.

LOOP AT ITAB.

AT NEW TXT.

I = I + 1.

ENDAT.

ITAB1-TXT = ITAB-TXT.

ITAB1-N = I.

APPEND ITAB1.

ENDLOOP.

LOOP AT ITAB1.

WRITE: / ITAB1-TXT, ITAB1-N.

ENDLOOP.

Former Member
0 Kudos

Hi clemens,

i have expand my example. if you will have the position of A or B or D.

DATA: BEGIN OF ITAB OCCURS 0,

TXT(1),

END OF ITAB.

DATA: BEGIN OF ITAB1 OCCURS 0,

TXT(1),

N(2) TYPE N,

END OF ITAB1.

*

DATA: BEGIN OF ITAB2 OCCURS 0,

TXT(1),

N(2) TYPE N,

END OF ITAB2.

*

DATA: I(2) TYPE N.

DATA: MOFF TYPE I.

*

ITAB-TXT = 'A'. APPEND ITAB.

ITAB-TXT = 'A'. APPEND ITAB.

ITAB-TXT = 'B'. APPEND ITAB.

ITAB-TXT = 'B'. APPEND ITAB.

ITAB-TXT = 'B'. APPEND ITAB.

ITAB-TXT = 'C'. APPEND ITAB.

ITAB-TXT = 'D'. APPEND ITAB.

ITAB-TXT = 'S'. APPEND ITAB.

*

LOOP AT ITAB.

AT NEW TXT.

I = I + 1.

ENDAT.

ITAB1-TXT = ITAB-TXT.

ITAB1-N = I.

APPEND ITAB1.

ENDLOOP.

*

LOOP AT ITAB.

ITAB2-TXT = ITAB-TXT.

FIND ITAB-TXT IN SY-ABCDE MATCH OFFSET MOFF.

ITAB2-N = MOFF + 1.

APPEND ITAB2.

ENDLOOP.

*

LOOP AT ITAB1. WRITE: / ITAB1-TXT, ITAB1-N. ENDLOOP.

LOOP AT ITAB2. WRITE: / ITAB2-TXT, ITAB2-N. ENDLOOP.

Hope it helps.

Regards, Dieter

Former Member
0 Kudos

Solved?

Regards, Dieter