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

itab

Former Member
0 Likes
1,028

how i do 2 lines instead of 3

matnr ............ menge

11111 10 4 3 3 3 3 10

<b>22222 10 3 3 3 3 3 5

22222 10 3 3 3 3 3 10</b>

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
997
Loop at itab.
   collect itab.
endloop

.

11 REPLIES 11
Read only

Former Member
0 Likes
998
Loop at itab.
   collect itab.
endloop

.

Read only

Former Member
0 Likes
997

try using Collect statement..

Regards..

Read only

Former Member
0 Likes
997

Use <b>Collect</b> Statement incase if you want to sum up

i.e, Loop at itab.

Collect itab.

endloop.

else USE <b>DELETE ADJACENT DUPLIACATES</b> statement for deleting the duplicate entries

Read only

Former Member
0 Likes
997

On what basis you want to merge and which columns?

What is the output you are looking at?

Regards,

Ravi

Read only

Former Member
0 Likes
997

Hi Rani,

I think you can use the statement DELETE ADJACENT DUPLICATE ENTRIES statement....

Just check this link...

http://help.sap.com/saphelp_nw04/helpdata/en/06/aafd54fc4011d195280000e8353423/content.htm

Regards,

SP.

Read only

Former Member
0 Likes
997

Hi rani,

can u please explain what exactly you are trying to do?

Regards,

Praveen Kumar

Read only

Former Member
0 Likes
997

Hi,

If your requirement is to add the last two lines and show as one line then use <b>collect statement</b> instead of append.

or

If your requirement is to take only one line based on duplicate entry then use <b>Delete adjascent entries statment.</b> on internal table.

<i>Hope This Info Helps YOU.</i>

Regards,

Lakshmi

Read only

rahulkavuri
Active Contributor
0 Likes
997

hi

Do u want to get only two entries based on the first 2 primary keys... then u can use

DELETE ADJACENT DUPLICATES COMPARING PM1 PM2.

Read only

Former Member
0 Likes
997
sort itab by matnr
itab_tmp[]  = itab[].
loop at itab_tmp.
collect itab_tmp.
endloop

.

here i am taking backup , if you want any further processing with it.

Regards

vijay

Read only

Former Member
0 Likes
997

hi rani,

use collect on itab

The following is the syntax for the collect statement.

collect [wa into] it.

where:

it is an internal table.

wa is a work area that has the same structure as it.

The following points apply:

If wa into is specified, the row to be collected is taken from the explicit work area wa. In this case, the header line of the internal table is ignored, if it has one.

If wa into is not specified, the internal table must have a header line. The row to be collected is taken from the header line for it.

When collect is executed, the system forms a key from the default key fields in the work area. The default key fields are the character fields (types c, n, d, t, and x). Therefore the key is composed of the values from all fields of type c, n, d, t, and x. It doesn't matter if they are beside one another or separated from each other by other fields.

The system then searches the body of the internal table for a row that has the same key as the key in the work area. If it doesn't find one, the row is appended to the end of the table. If it does find one, the numeric fields (types i, p, and f) in the work area are added to the corresponding fields in the found row. Listing 12.11 illustrates this concept.

-


Listing 12.11 collect Combines Rows as They Are Added to an Internal Table

1 report ztx1211.

2 data: begin of it occurs 10,

3 date type d, " part of default key

4 tot_sales type p decimals 2, "not part of default key

5 name(10), " part of default key

6 num_sales type i value 1, "not part of default key

7 end of it.

8

9 it-date = '19980101'.

10 it-tot_sales = 100.

11 it-name = 'Jack'.

12 collect it.

13

14 it-date = '19980101'.

15 it-tot_sales = 200.

16 it-name = 'Jim'.

17 collect it.

18

19 it-date = '19980101'.

20 it-tot_sales = 300.

21 it-name = 'Jack'.

22 collect it.

23

24 it-date = '19980101'.

25 it-tot_sales = 400.

26 it-name = 'Jack'.

27 collect it.

28

29 it-date = '19980101'.

30 it-tot_sales = 500.

31 it-name = 'Jim'.

32 collect it.

33

34 it-date = '19980101'.

35 it-tot_sales = 600.

36 it-name = 'Jane'.

37 collect it.

38

39 it-date = '19980102'.

40 it-tot_sales = 700.

41 it-name = 'Jack'.

42 collect it.

43

44 loop at it.

45 write: / it-date, it-tot_sales, it-name, it-num_sales.

46 endloop.

-


The code in Listing 12.11 produces this output:

19980101 800.00 Jack 3

19980101 700.00 Jim 2

19980101 600.00 Jane 1

19980102 700.00 Jack 1

Lines 2 through 7 define an internal table that has four components. Two are character types (date and name) and two are numeric types (tot_sales and num_sales). The default key is therefore composed of the components date and name.

Lines 9 through 11 assign values to the header line components date, tot_sales, and name. num_sales still has a default value of 1, assigned on line 6.

Line 12 searches the body of it for a row that has the same values in date and name (the default key fields) as those in the header line. The internal table is empty, so no rows match. The header line is therefore appended to the internal table (see Figure 12.7).

Figure 12.7 : If the default key is not found in the body, the row is appended. The default key is composed of all non-numeric fields, so in this diagram it is the combination of date (first field) and name (third field).

Line 17 behaves like line 12. The internal table has one row, but the values in the date and name fields don't match, so the header line is appended to the internal table (see Figure 12.8).

Figure 12.8 : Again, the default key is not found in the table, so this row is also appended.

Line 22 searches the internal table for a row that has '19980101' in the date field and 'Jack' in the name field. Row 1 matches, so the numeric fields are added together. The header line tot_sales field contains 200. This is added to the tot_sales field in row 1, yielding a total of 300. The value in the num_sales field in the header line is also added to the num_sales field in row 1, giving a value of 2. Row 1 is updated with these values. The contents of the header line are unchanged (see Figure 12.9).

Figure 12.9 : This time the default key matches the first row of the table. The numeric fields in the work area are added to the corresponding fields in the first row.

Line 27 searches for a row with a date of '19980101' and a name of 'Jack'. Row 1 matches, so the contents of the numeric fields (tot_sales and num_sales) in the header line are added to the numeric fields in the found row (see Figure 12.10).

Figure 12.10: Again, the default key matches the first row of the table. The numeric fields in the work area are added to the corresponding fields in the first row.

Line 32 matches on row 2 and adds '500' to tot_sales and 1 to num_sales (see Figure 12.11).

Figure 12.11: This time the default key matches the second row of the table. The numeric fields in the work area are added to the corresponding fields in the second row.

On line 37, the values in date and name (the default key fields) of the header line do not match any rows in it, so a new row is appended to the end of the table (see Figure 12.12).

Figure 12.12: The default key does not match any rows this time, so a new row is appended.

Line 42 also causes a new row to be appended to the internal table because the values in date and name do not match in any rows of it (see Figure 12.13).

Figure 12.13: The default keys again do not match any rows, so another row is appended.

CAUTION

If you use collect to add rows to an internal table, all rows should be added using collect. You should not combine collect with append or any other statement that adds data to an internal table. The results will be unpredictable. The only other statement that you can use to modify the contents of an internal table filled via collect is modify ... transporting f1 f2 ..., where f1 and f2 are numeric fields only (non-default key fields).

regards,

keerthi.

Read only

Former Member
0 Likes
997

hi rani,

use either <b>collect</b>stmt

or .<b>delete adjacent duplicate entries</b>.

thanks,

priya