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

problem joining 5 tables

Former Member
0 Likes
694

Hi, I have to modify a report because it goes too slow.. the problem is when joining 5 tables, i'm trying to change it but i'm a little bit "lost".... the original code is de following..

SELECT DISTINCT aaufnr aauart aerdat aktext a~werks

bacpos bobjnr

ckokrs cacpos

dbwart dbudat dmblnr dmjahr dzeile derfmg d~erfme

dmatnr emaktx daufnr ddmbtr dkokrs dsakto

INTO TABLE it_materiales

FROM ( aufk AS a

INNER JOIN pmco AS b

ON bobjnr = aobjnr

INNER JOIN tpik3 AS c

ON ckokrs = akokrs

AND cacpos = bacpos

INNER JOIN aufm AS d

ON daufnr = aaufnr

AND dkokrs = ckokrs

AND dsakto = ckstrf

INNER JOIN makt AS e

ON ematnr = dmatnr )

WHERE a~aufnr IN s_aufnr AND

a~auart IN s_auart AND

a~werks IN s_iwerk AND

b~acpos IN s_acpos AND

d~budat IN s_budat AND

e~spras = sy-langu.

to make it go quicklier, i did this:

DATA: it_aufm LIKE aufm OCCURS 0 WITH HEADER LINE,

it_tpik3 LIKE tpik3 OCCURS 0 WITH HEADER LINE,

it_aux LIKE it_materiales OCCURS 0 WITH HEADER LINE,

wa_material LIKE it_materiales.

SELECT DISTINCT aaufnr aauart aerdat aktext a~werks

bacpos bobjnr

INTO CORRESPONDING FIELDS OF TABLE it_materiales

FROM ( aufk AS a

INNER JOIN pmco AS b

ON bobjnr = aobjnr )

WHERE a~aufnr IN s_aufnr AND

a~auart IN s_auart AND

a~werks IN s_iwerk AND

b~acpos IN s_acpos.

SELECT * INTO TABLE it_tpik3

FROM tpik3

FOR ALL ENTRIES IN it_materiales

WHERE kokrs = it_materiales-kokrs

AND acpos = it_materiales-acpos.

LOOP AT it_materiales INTO wa_material.

LOOP AT it_tpik3 WHERE kokrs = wa_material-kokrs

AND acpos = wa_material-acpos.

MOVE-CORRESPONDING wa_material TO it_aux.

MOVE-CORRESPONDING it_tpik3 TO it_aux.

APPEND it_aux.

CLEAR it_aux.

ENDLOOP.

ENDLOOP.

it_materiales[] = it_aux[].

LOOP AT it_materiales.

READ TABLE it_aufm WITH KEY aufnr = it_materiales-aufnr

kokrs = it_materiales-kokrs

sakto = it_materiales-kstrf.

IF sy-subrc = 0.

IF it_aufm-budat NOT IN s_budat.

DELETE it_materiales.

ELSE.

SELECT SINGLE maktx

FROM makt

INTO it_materiales-maktx

WHERE matnr = it_materiales-matnr

AND spras = sy-langu.

MODIFY it_materiales.

ENDIF.

ENDIF.

ENDLOOP.

when acceding table TPIK3 i get no data.... i know i'm doing something wrong but don't know where....

thanks in advance !!!

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
629

Hi,

Try this:

1. check join conditions properly.

2. do not use use nested loop command s like

LOOP AT it_materiales INTO wa_material.

LOOP AT it_tpik3 WHERE kokrs = wa_material-kokrs

.

.

ENDLOOP.

ENDLOOP.

instead use.

loop at it_material..

read table it_tpik3 with key....

.

.

.

endloop.

3. do not use CORROSPONDING FIELDS option!!!

4. donot use select single in loop, instead fetch all records once in itabl using for all entries.

5. check data of it_material with these condition in table tpik3 :

WHERE kokrs = it_materiales-kokrs

AND acpos = it_materiales-acpos.

Hope this will help you.

Jogdand M B

3 REPLIES 3
Read only

Former Member
0 Likes
630

Hi,

Try this:

1. check join conditions properly.

2. do not use use nested loop command s like

LOOP AT it_materiales INTO wa_material.

LOOP AT it_tpik3 WHERE kokrs = wa_material-kokrs

.

.

ENDLOOP.

ENDLOOP.

instead use.

loop at it_material..

read table it_tpik3 with key....

.

.

.

endloop.

3. do not use CORROSPONDING FIELDS option!!!

4. donot use select single in loop, instead fetch all records once in itabl using for all entries.

5. check data of it_material with these condition in table tpik3 :

WHERE kokrs = it_materiales-kokrs

AND acpos = it_materiales-acpos.

Hope this will help you.

Jogdand M B

Read only

Former Member
0 Likes
629

ok.. i'll make some changes but.... the join sentence i don't know how to do it properly, i've done it as it was done before... no primary key fields...i have done a nested loop because i thought there could be more than one registry for an entry in it_materiales...

and the biggest doubt i have is why it_tpik3 has no data aftee the "select .. for all entries in it_materiales.." ??

thanks...

Read only

0 Likes
629

Hi Maria,

If you are access 5 database table with join, it will affect performance of the report. It always recommand that select from main table and use 'FOR ALL ENTRIES' and then manipulate the information as per required using loop.

Regards

Nilesh Shete