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

Issue with data collection from internal tables

Former Member
0 Likes
1,037

Hi experts,

I want to constitute with an abap code one globe internal table form many other internal tables. To simplify my example let’s say 3 inernal tables.

The key of the internal tables will be field1. I have to take the records only one time with the help of this field, if I find it in another table I don’t have to take it.

But I have to take it as many times I found them form the same table.

Here the example:

Internal table 1 (itable1):

Field1

Field2

Field3

A

X

10

A

Y

35

B

Y

20

C

Z

30

Internal table 2 (itable2):

Field1

Field2

Field3

A

X

50

A

Y

30

C

Z

3

Internal table 3 (itable3):

Field1

Field2

Field3

D

Z

22

A

Z

12

C

Z

100

So the global internal table (iglobal) has to be as following:

Field1

Field2

Field3

A

X

10

A

Y

35

B

Y

20

C

Z

30

D

Z

22

Thanks for your suggestions.

Amine

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,004

So here the logic to solve this:

Data : iglobal type sorted table of XXX with non unique key field1.

Data:  itable1 type table of XXX.

Data:  itable2 type table of XXX.

Data:  itable3 type table of XXX.

“…SQL treatments

Sort itable1 by field1.

Loop at itable1 into structure.

At new field1.

Read table iglobal with key field1 = structure-field1 transporting no fields.

If sy-subrc ne 0.

Insert structure into iglobal.

                                  Endif.

             Endat.

Endloop.


Same loops for itable2 and itable3.

But the issue,  this logic doesn’t manage the case where a record is present several times in the same table which I sould take. I’m stuck with it, and the result is like this (it doesen’t take in account line number 2 in my example):

Field1

Field2

Field3

A

X

10

A

Y

35

B

Y

20

C

Z

30

D

Z

22

Thanks.

Amine

Hi experts,

I want to constitute with an abap code one globe internal table form many other internal tables. To simplify my example let’s say 3 inernal tables.

The key of the internal tables will be field1. I have to take the records only one time with the help of this field, if I find it in another table I don’t have to take it.

But I have to take it as many times I found them form the same table.

Here the example:

Internal table 1 (itable1):

Field1

Field2

Field3

A

X

10

A

Y

35

B

Y

20

C

Z

30

Internal table 2 (itable2):

Field1

Field2

Field3

A

X

50

A

Y

30

C

Z

3

Internal table 3 (itable3):

Field1

Field2

Field3

D

Z

22

A

Z

12

C

Z

100

So the global internal table (iglobal) has to be as following:

Field1

Field2

Field3

A

X

10

A

Y

35

B

Y

20

C

Z

30

D

Z

22

Thanks for your suggestions.

Amine

9 REPLIES 9
Read only

Former Member
0 Likes
1,005

So here the logic to solve this:

Data : iglobal type sorted table of XXX with non unique key field1.

Data:  itable1 type table of XXX.

Data:  itable2 type table of XXX.

Data:  itable3 type table of XXX.

“…SQL treatments

Sort itable1 by field1.

Loop at itable1 into structure.

At new field1.

Read table iglobal with key field1 = structure-field1 transporting no fields.

If sy-subrc ne 0.

Insert structure into iglobal.

                                  Endif.

             Endat.

Endloop.


Same loops for itable2 and itable3.

But the issue,  this logic doesn’t manage the case where a record is present several times in the same table which I sould take. I’m stuck with it, and the result is like this (it doesen’t take in account line number 2 in my example):

Field1

Field2

Field3

A

X

10

A

Y

35

B

Y

20

C

Z

30

D

Z

22

Thanks.

Amine

Read only

0 Likes
1,004

Take everything from itable1 into your iglobal. Then append the iglobal1 from table itable2 and itable3 where iglobal1-field1 NE itable2-field1 and iglobal1-field1 NE itable3-field1..

Read only

0 Likes
1,004

Hi Amine,

In your case I would have chosen a little different approach.Since you want records from first table to repeat, and in your read you are checking for the key before inserting, this second record will be treated as ''duplicate'' and will never insert.

Please try and test if it works for you.

my suggestion:

Sort itable1 by field1.

Loop at itable1 into structure.

*- below delete statements will delete the records added from the first table from other two tables

Delete ITABLE2 where FIELD1 = structure-FIELD1. 

Delete ITABLE3 where FIELD1 = structure-FIELD1.

*- now insert all the records from ITABLE1.

Insert structure into iglobal.

            

Endloop.

Similarly, For loop at ITABLE2, this table will have only entries not present in ITABLE1, so go on and include all the entries from this table but deleting those records from ITABLE3. This way you will be left with an ITABLE3 having records that arent present in either ITABL1 or ITABLE2.

Please let me know if this helps you in any way.

Regards,

DN.

Read only

0 Likes
1,004

Hi Amine,

Use the following code:

iglobal = itab1.

LOOP AT itab2 INTO wa2.

   READ TABLE iglobal WITH KEY field1 = wa2-field1 TRANSPORTING NO FIELDS.

   IF sy-subrc <> 0.

       INSERT wa2 INTO TABLE iglobal.

   ENDIF.

   CLEAR wa2.

ENDLOOP.

LOOP AT itab3 INTO wa3.

   READ TABLE iglobal WITH KEY field1 = wa3-field1 TRANSPORTING NO FIELDS.

   IF sy-subrc <> 0.

       INSERT wa3 INTO TABLE iglobal.

   ENDIF.

ENDLOOP.

Read only

0 Likes
1,004

Hi Amine,

I believe the above solution from Deepak will do the trick if you want to do in the application server. OR, you could select the right records from the database using sub queries. It might be not very good performance wise depending on your tables key/index/volume, etc. But for my tests with the data you provided was perfect

my data:

tab1

AX10
AY35
BY20
CZ30

tab2

AX50
CZ3
AY30

tab3

AZ12
CZ100
DZ22

The code:

* Select every record

SELECT field1 field2 field3

        FROM ytab1 INTO TABLE tab.

* do not select what is in tab1

SELECT field1 field2 field3

        FROM ytab2

        APPENDING TABLE tab

        WHERE field1 NOT IN ( SELECT field1 FROM ytab1 ).

* do not select what is either in tab1 or tab2

SELECT field1 field2 field3

        FROM ytab3

        APPENDING TABLE tab

        WHERE field1 NOT IN ( SELECT field1 FROM ytab1 )

          AND field1 NOT IN ( SELECT field1 FROM ytab2 ) .

the result:

Cheers,

Custodio

@zcust01

Read only

anup_deshmukh4
Active Contributor
0 Likes
1,004

Hello Amine,

will Merging the daa Sorting and using 'DELETE ADJACENT DUPLICATES COMPARING....' ?

Read only

0 Likes
1,004

This message was moderated.

Read only

Former Member
0 Likes
1,004

hi Aneesh,

just use collect statement instead of append i think it will solve ur query.

just check with below code

please ignore syntax.

loop at itab1.

loop at itab2 where key field1 = itab1-field1 field2 = tab1-field.

collect itab2 into itab1.

endloop.

loop at itab3 where key field1 = itab1-field1 field2 = itab1-field.

collect itab3 into itab1.

endloop.

endloop.

Regards,

Gaurav Wani

Read only

Former Member
0 Likes
1,004

Thanks to all of you guys.

All the ideas were great, the advantage of the one proposed by Custodio is that i can continue with the same logic if i have much more tables...

Amine