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 with join logic

Former Member
0 Likes
1,795

I am attaching the code that I am using to retrieve data for customers. We have two customers on the KNB1 table, each with a different company code. When I run my process, and enter a company code, I am getting both records instead of the one that matches the company code that I entered. I have tried different variations of the inner join statement between the KNA1 and KNB1 tables and also the select sttement but I cannot get to retrieve only the record for the selected company.

thanks in advance for the help

SELECT

KNA1KUNNR KNB1BUKRS KNVVVKORG KNVVVTWEG KNVVSPART KNA1KTOKD

KNVVKKBER ADRCTITLE ADRCNAME1 ADRCNAME2 ADRCNAME3 ADRCNAME4

ADRCSORT1 ADRCSORT2 ADRCBUILDING ADRCROOMNUMBER ADRC~FLOOR

ADRCNAME_CO ADRCSTR_SUPPL1 ADRCSTR_SUPPL2 ADRCSTREET ADRC~HOUSE_NUM1

ADRCHOUSE_NUM2 ADRCSTR_SUPPL3 ADRCLOCATION ADRCCITY2 ADRC~HOME_CITY

ADRCPOST_CODE1 ADRCCITY1 ADRCCOUNTRY ADRCREGION ADRC~TIME_ZONE

ADRCTAXJURCODE ADRCTRANSPZONE ADRCREGIOGROUP ADRCDONT_USE_S

ADRCPO_BOX ADRCPO_BOX_NUM ADRCPOST_CODE2 ADRCPO_BOX_LOC

ADRCPO_BOX_CTY ADRCPO_BOX_REG ADRCPOST_CODE3 ADRCDONT_USE_P

ADRCLANGU ADRCTEL_NUMBER ADRCTEL_EXTENS KNA1TELF2

ADRCFAX_NUMBER ADR6SMTP_ADDR ADRCDEFLT_COMM ADRCTREMARK

KNA1LIFNR KNA1BEGRU KNA1VBUND KNA1KONZS KNA1BBBNR KNA1BBSNR

KNA1BUBKZ KNA1BRSCH KNA1LZONE KNA1LOCCO KNA1STCD1 KNA1STCD2

KNA1STKZA KNA1STKZN KNA1STKZU KNA1FISKN KNA1COUNC KNA1CITYC

KNA1STCEG KNA1TXJCD KNA1KNRZA KNA1XZEMP KNZAEMPFD KNA1NIELS

KNA1RPMKR KNA1KUKLA KNA1HZUOR KNA1BRSCH KNA1BRAN1 KNA1BRAN2

KNA1BRAN3 KNA1BRAN4 KNA1BRAN5 KNA1UMSA1 KNA1UWAER KNA1UMJAH

KNA1JMZAH KNA1JMJAH KNA1PERIV KNA1GFORM KNB1AKONT KNB1ZUAWA

KNB1KNRZE KNB1BEGRU KNB1FDGRV KNB1WBRSL KNB1ALTKN KNB1PERNR

KNB1EKVBD KNB1ZTERM KNB1TOGRU KNB1URLID KNB1WAKON KNB1CESSION_KZ

KNB1XZVER KNB1KULTG KNB1ZWELS KNB1ZAHLS KNB1KNRZB KNB1HBKID

KNB1WEBTR KNB1ZGRUP KNB1XPORE KNB1XVERR KNB1REMIT KNB1XEDIP

KNB1LOCKB KNB1XKNZB KNB5MAHNA KNB5MANSP KNB5KNRMA KNB5GMVDT

KNB5MADAT KNB5MAHNS KNB5BUSAB KNB1MGRUP KNB1BUSAB KNB1XAUSZ

KNB1EIKTO KNB1PERKZ KNB1ZSABE KNB1TLFNS KNB1TLFXS KNB1INTAD

KNB1KVERM KNB1ZAMIM KNB1ZAMIV KNB1ZAMIR KNB1ZAMIO KNB1ZAMIB

KNVVBZIRK KNVVAWAHR KNVVVKBUR KNVVBEGRU KNVVVKGRP KNVVVSORT

KNVVKDGRP KNVVEIKTO KNVVKLABC KNVVMEGRU KNVVWAERS KNVVKURST

KNVVRDOFF KNVVPVKSM KNVVKONDA KNVVKALKS KNVVPLTYP KNVVVERSG

KNVVAGREL KNVVBLIND KNVVLPRIO KNVVKZAZU KNVVVSBED KNVVCHSPL

KNVVVWERK KNVVPODKZ KNVVPODTG KNVVAUTLF KNVVKZTLF KNVVANTLF

KNVVUEBTK KNVVUNTTO KNVVUEBTO KNA1LZONE KNA1BAHNE KNA1BAHNS

KNVVMRNKZ KNVVBOKRE KNVVPRFRE KNVVPERFK KNVVPERRL KNVVINCO1

KNVVINCO2 KNVVZTERM KNVVKTGRD KNVIALAND KNVITATYP KNVITAXKD

KNVLLICNR KNVLBELIC KNVLDATAB KNVLDATBI KNVP~PARVW

KNVKPARNR KNVPDEFPA KNVP~KNREF

INTO TABLE it_sap_data

FROM ( kna1 INNER JOIN knb1 ON kna1kunnr EQ knb1kunnr )

LEFT OUTER JOIN knbk ON kna1kunnr EQ knbkkunnr

LEFT OUTER JOIN knvk ON kna1kunnr EQ knvkkunnr

LEFT OUTER JOIN adrc ON kna1adrnr EQ adrcaddrnumber

LEFT OUTER JOIN adrct ON kna1adrnr EQ adrctaddrnumber

LEFT OUTER JOIN knvv ON kna1kunnr EQ knvvkunnr

LEFT OUTER JOIN knza ON kna1kunnr EQ knzakunnr

LEFT OUTER JOIN knb5 ON kna1kunnr EQ knb5kunnr

LEFT OUTER JOIN knvi ON kna1kunnr EQ knvikunnr

LEFT OUTER JOIN knvl ON kna1kunnr EQ knvlkunnr

LEFT OUTER JOIN knvp ON kna1kunnr EQ knvpkunnr

LEFT OUTER JOIN adr6 ON kna1adrnr EQ adr6addrnumber

WHERE knb1~KUNNR IN so_kunnr AND

knb1~bukrs IN so_cocod AND

knb1~altkn IN so_altkn.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,402

It's a pretty complicated SELECT. The first thing I would do is simplify it (for testing purposes) to only look at KNA1 and KNB1.

Rob

8 REPLIES 8
Read only

Former Member
0 Likes
1,402

Did you tried <b>SELECT DISTINCT</b>???

Greetings,

Blag.

Read only

0 Likes
1,402

no I did not. If the user does not enter the company code, the program should return all the transactions. If the user enters the company code, only the record for that company code should be returned

Read only

Former Member
0 Likes
1,403

It's a pretty complicated SELECT. The first thing I would do is simplify it (for testing purposes) to only look at KNA1 and KNB1.

Rob

Read only

0 Likes
1,402

I have simplified the select and found out that the problem is with the KNB5 table. this table is similar to KNB1 in that there are 2 entries for the same customer but with different company codes. Is there a way for me to select only the KNB5 transaction that matches the company code that I select. I have tried to put it in the WHERE section of the select but this is not correct.

Read only

0 Likes
1,402

You can't add a criteria in the WHERE clause of a left-outer joinned table.

I suggest you, if you can to "iNNER JOIN" KNB5 or to exit this table from your Join.

Erwan

Read only

0 Likes
1,402

I changed the join statement from outer join to

<b>INNER JOIN knb5 ON kna1kunnr EQ knb5kunnr</b>

and I added the following to the WHERE statement

<b> knb5~bukrs IN so_cocod AND</b>

It looks like it is selecting the correct data.

Read only

0 Likes
1,402

when I run the process with the new code for the inner join and the where statement, If I select the company code, the process works correctly, If I leave the company code blank, instead of getting the two records that I would expect, I am now getting 4 records. I am thinking that there are 2 in the KNB1 table and 2 in the KNB5 table and the inner join is causing this. Is there a way to correct this?

Read only

0 Likes
1,402

I removed the join statement for knb5 and created a little section where I select the records separately.

thanks for all he help