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

Problems with joining tables

Former Member
0 Likes
1,113

Hello,

I am totally new at programming abap and would like som help with a join-statement.

I am wondering why this code generates four rows instead of 2 as a result? Is there some kind of nested loops going on? Can I solve this with inner or outer joins?

The code:

Report test.

TYPES: BEGIN OF itab,

ltxa1 LIKE afvc-ltxa1,

vornr LIKE afvc-vornr,

arbei LIKE afvv-arbei,

END OF itab.

DATA: it_temp TYPE table of itab,

wa_temp TYPE itab.

SELECT altxa1 avornr c~arbei

INTO (wa_temp-ltxa1, wa_temp-vornr, wa_temp-arbei)

FROM afvc AS a

JOIN caufv AS b

ON aaufpl = baufpl

OUTER JOIN afvv AS c

ON baufpl = caufpl

WHERE b~aufnr = '000005000240'.

WRITE:/ wa_temp-ltxa1, wa_temp-vornr, wa_temp-arbei.

APPEND wa_temp TO it_temp.

ENDSELECT.

Regards

Tomas

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,054

Hi Tomas,

Change it this way..

SELECT altxa1 avornr c~arbei

INTO (wa_temp-ltxa1, wa_temp-vornr, wa_temp-arbei)

FROM afvc AS a

JOIN caufv AS b

ON aaufpl = baufpl

<b>JOIN</b> afvv AS c

ON baufpl = caufpl

WHERE b~aufnr = '000005000240'.

WRITE:/ wa_temp-ltxa1, wa_temp-vornr, wa_temp-arbei.

APPEND wa_temp TO it_temp.

ENDSELECT.

9 REPLIES 9
Read only

Former Member
0 Likes
1,055

Hi Tomas,

Change it this way..

SELECT altxa1 avornr c~arbei

INTO (wa_temp-ltxa1, wa_temp-vornr, wa_temp-arbei)

FROM afvc AS a

JOIN caufv AS b

ON aaufpl = baufpl

<b>JOIN</b> afvv AS c

ON baufpl = caufpl

WHERE b~aufnr = '000005000240'.

WRITE:/ wa_temp-ltxa1, wa_temp-vornr, wa_temp-arbei.

APPEND wa_temp TO it_temp.

ENDSELECT.

Read only

0 Likes
1,054

Nope, changing outer join to join didnt help.

I still get four rows as a result instead of 2

Read only

0 Likes
1,054

Please see my previous post.

Regards,

Rich Heilman

Read only

0 Likes
1,054

Thanks a lot Rich! This did the job.

Now it works great. This forum is truly amazing, and you guys are great. Keep it up.

Can you explain your code to me? This aplzl field, is it an internal counter or what does it do?

Regards

Tomas

Read only

0 Likes
1,054

The reason that you need that is because of the relation between AFVC and AFVV. Notice that the keys of these tables are the same. Meaning that for every AFVC record, there is a corresponding AFVV record and they are joined by there keys AUFPL and APLZL. The way you had you code earlier, you were joining every record from AFVV with key AUFPL to each record of AFVC with key AUFPL. Now you are joining 1 for 1 to the tables. Did I confuse you more?

Regards,

Rich Heilman

Read only

0 Likes
1,054

Hello again,

Thanks for the code phani, great.

Also huge thanks for the explanation Rich. You didnt confuse me at all This made understand why I got four rows.

Regards

Tomas

Read only

0 Likes
1,054

Hi Tomas,

Generally when you are selecting data from the tables make sure you take the key fields in your select,so that you will not get confused when you get multiple lines of similar data.If the key is present then it is quite easy to understand the data in the internal table.

While writing as a report,you can just write those fields which are required.

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,054

I would suggest that you change your code to something like this. This works a lot better in my system.

This most important part here is that you are missing a key when join the AFVC and AFVV tables. You need to add APLZL.



report zrich_0003.


types: begin of itab,
ltxa1 like afvc-ltxa1,
vornr like afvc-vornr,
arbei like afvv-arbei,
end of itab.

data: it_temp type table of itab,
      wa_temp type itab.

select<b> b~ltxa1 b~vornr c~arbei</b>into (wa_temp-ltxa1, wa_temp-vornr, wa_temp-arbei)
<b>   from caufv as a
       inner join afvc as b
        on a~aufpl = b~aufpl</b>
          inner join afvv as c
                 on b~aufpl = c~aufpl
<b>                and b~APLZL = C~APLZL</b>     " <--- This right here  "
                       <b>where a~aufnr = '001004193793'.</b>
  write:/ wa_temp-ltxa1, wa_temp-vornr, wa_temp-arbei.
  append wa_temp to it_temp.
endselect.

Also, I have swapped the join for CAUFV and AFVC.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
1,054

Hi Tomas,

Change your internal table & populate the values like this to see whether your data is duplicating or not.

TYPES: BEGIN OF itab,

aufnr like caufv-aufnr,

aufpl like afvc-aufpl,

ltxa1 LIKE afvc-ltxa1,

vornr LIKE afvc-vornr,

arbei LIKE afvv-arbei,

END OF itab.

SELECT aaufnr baufpl altxa1 avornr c~arbei

INTO (wa_temp-aufnr, wa_temp-aufpl, wa_temp-ltxa1, wa_temp-vornr, wa_temp-arbei)

FROM afvc AS a

JOIN caufv AS b

ON aaufpl = baufpl

OUTER JOIN afvv AS c

ON baufpl = caufpl

<b>baplzl = caplzl</b>

WHERE b~aufnr = '000005000240'.

WRITE:/ wa_temp-aufnr, wa_temp-aufpl, wa_temp-ltxa1, wa_temp-vornr, wa_temp-arbei.

APPEND wa_temp TO it_temp.

ENDSELECT.

Message was edited by: Phani Kiran Nudurupati