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: 

Query on internal table

0 Kudos

Hi All,

Please resolve the following quiery:

The internal table itab1 is as follows;

A B C

1 X 12

2 X 11

3 X 9

4 Y 1

5 Y 3

6 Y 5

7 Z 22

8 Z 4

9 Z 7

10 Z 8

The totals of all values corresponding to x,y and z are sotred as follows in itab2:

A B c

X 32

Y 9

Z 41

Now the above two tables has to be combined and display the output in below format:

That is the total correspnding to X should come first before the X vaues are started.

That is the total correspnding to Y should come first before the Y vaues are started.

That is the total correspnding to Z should come first before the Z vaues are started.

The final output ih itab3 is as follows:

A B C

X 32

1 X 12

2 X 11

3 X 9

Y 9

4 Y 1

5 Y 3

6 Y 5

Z 41

7 Z 22

8 Z 4

9 Z 7

10 Z 8

Please do the needful at the earliest.

Regards,

A.Srinivas

1 ACCEPTED SOLUTION

0 Kudos

<< removed >>

Edited by: Rob Burbank on Dec 6, 2009 5:32 PM

10 REPLIES 10

0 Kudos

<< removed >>

Edited by: Rob Burbank on Dec 6, 2009 5:32 PM

0 Kudos

Hi,

Please try this:-

SORT itab1 BY B.

Loop at itab1 into wa_itab1.

AT NEW B.

READ TABLE itab2 into wa_itab2 with key A = wa_itab1-B.

IF sy-subrc = 0.

wa_itab3-A = WA_ITAB2-A.

wa_itab3-B = wa_itab2-B.

ENDIF.

ENDAT.

wa_itab3-A = wa_itab1-A.

wa_itab3-B = wa_itab1-B.

wa_itab3-C = wa_itab1-C.

append wa_itab3 to t_itab3.

endloop.

Regards,

Subhashini

0 Kudos

You need to loop on first table, then read records from second table, not fill the third internal table as desired.

Press F1 to AT New to fill the first row.

Hope you understand it.

0 Kudos

Hi,

I feel the code which Subhasini K has given is almost correct. Just one thing I would like to add.

In the AT..ENDAT. you have to add another line

append wa_itab3 to t_itab3.

So the revised AT..ENDAT should look like -

AT NEW B.

READ TABLE itab2 into wa_itab2 with key A = wa_itab1-B.

IF sy-subrc = 0.

wa_itab3-A = WA_ITAB2-A.

wa_itab3-B = wa_itab2-B.

append wa_itab3 to t_itab3.

ENDIF.

ENDAT.

Rest should be same.

Please let me know if you problem is not resolved.

Thanks,

Mainak

0 Kudos

Hi,

Thanks for your quick and prospective reply.

The actual issues as follows:

The internal table itab1 is as follows;

A B C

1 X 12

2 X 11

3 X 9

4 Y 1

5 Y 3

6 Y 5

7 Z 22

8 Z 4

9 Z 7

10 Z 8

The totals of all values corresponding to x,y and z are sotred as follows in itab2:

A B c

X 32

Y 9

Z 41

Now the above two tables has to be combined and display the output in below format:

That is the total correspnding to X should come first before the X vaues are started.

That is the total correspnding to Y should come first before the Y vaues are started.

That is the total correspnding to Z should come first before the Z vaues are started.

The final output ih itab3 is as follows:

A B C

X 32

1 X 12

2 X 11

3 X 9

Y 9

4 Y 1

5 Y 3

6 Y 5

Z 41

7 Z 22

8 Z 4

9 Z 7

10 Z 8

***********************************************************************************************************

Now my above same issue is extended as follows:

The new internal table is as follows:

The internal table itab1 is as follows;

A B B1 C

1 X cas 12

2 X IBI 11

3 X IBI 9

4 Y cas 1

5 Y cas 3

6 Y cas 5

7 Z cas 22

8 Z cas 4

9 Z cas 7

10 Z cas 8

The totals of all values corresponding to x,y and z are sotred as follows in itab2 using along collect function along with new field B1:

A B B1 C

X 12 cas

X 20 IBI

Y 9 cas

Z 41 cas

Now the above two tables has to be combined and display the output in below format:

That is the total correspnding to X and cas should come first before the X values(corresponding to cas) are started.

That is the total correspnding to X and IBI should come first before the X values(corresponding to IBI) are started.

That is the total correspnding to Y should come first before the Y vaues are started.

That is the total correspnding to Z should come first before the Z vaues are started.

The final output ini itab3 should be as follows:

A B B1 C

X cas 12

1 X IBI 12

X IBI 20

2 X IBI 11

3 X IBI 9

Y cas 9

4 Y cas 1

5 Y cas 3

6 Y cas 5

Z cas 41

7 Z cas 22

8 Z cas 4

9 Z cas 7

10 Z cas 8

0 Kudos

Hi,

Please do the needful to my extended issue at the earliest.

Regards,

A.Srinivas

0 Kudos

Hi,

Please do this:-

SORT itab1 BY B B1.

Loop at itab1 into wa_itab1.

AT NEW B B1.

READ TABLE itab2 into wa_itab2 with key A = wa_itab1-B

B1 = wa_itab1-B1.

IF sy-subrc = 0.

wa_itab3-A = WA_ITAB2-A.

wa_itab3-B = wa_itab2-B.

wa_itab3-B1 = wa_itab2-B1.

append wa_itab3 to t_itab3.

ENDIF.

ENDAT.

if sy-subrc <> 0.

wa_itab3-A = wa_itab1-A.

wa_itab3-B = wa_itab1-B.

wa_itab3-B1 = wa_itab1-B1.

wa_itab3-C = wa_itab1-C.

append wa_itab3 to t_itab3.

endif.

endloop.

Regards,

Subhashini

0 Kudos

Hi,

Please can you update on this issue.

Regards,

Subhashini

0 Kudos

Hi,

I am just waiting for any further extension of my issue so as to update you the same at once.

Thanks for your reply and solution but there is no possbitility for your second solution as "at new takes" only one field at a time.

Your first solution gave me a lot of inputs for initiation of my issue.

I resolved the same by "on change of b or b1."

Thanks again for all your help.

Regards,

A.Srinivas

SureshRa
Active Participant
0 Kudos

Hi Srinu,

The following logic should work to meet your requirement:

  • Assuming all int tables with header line

LOOP AT itab2.

APPEND itab2 to itab3. "This will give you summary line

  • Then conditionally loop through itab1

LOOP AT itab1 WHERE b = itab2-a.

APPEND itab1 to itab3.

ENDLOOP.

ENDLOOP.

*Now you got your itab3 as required

Regards

Suresh