‎2009 Mar 24 9:28 AM
Hi,
I have 3 tables with following key field values (column is kalled id):
ztab1: one, two, three, four
ztab2: one, two, three
ztab3::one, two
I want to INNER JOIN table 2 and 3 and I want to LEFT OUTER JOIN table 1 and 2. So the result set I am expecting is values one, two, three, four.
This is my select that is not working but only returning one,two, three (four is missing).
SELECT ztab1~id
FROM ztab1 LEFT OUTER JOIN ztab2
ON ztab1~id = ztab2~id INNER JOIN ztab3
ON ztab2~id = ztab3~idWhy is this returning only one, two, three and not the four as well and how can I rewrite this select to that it works?
thanks
‎2009 Mar 24 9:30 AM
The "ON" keyword compares the id -> FOUR from itab1 and it doesnt find any FOUR in itab 2.
‎2009 Mar 24 9:33 AM
Hi,
SELECT ztab1~id
FROM ztab2 INNER JOIN ztab3
ON ztab2id = ztab3id
LEFT OUTER JOIN ztab3
ON ztab1id = ztab2id
Try the above...
Best Regards,
Suresh
‎2009 Mar 24 10:08 AM
Unfortunately none of the above answers are correct.
I want to put up a more general question then. Say you have 3 tables where table 1 > 2 > 3
meaning 1 has more rows than 2, 2 have more rows than 3.
Now, left join table 1 and 2 and inner join table 2 and 3.
What would the resultset be if you wrote this in one single abap statement?
My guess is it should be all the rows from table 1 since the LEFT JOIN statements will disregard that there are no rows on the right hand side that matches.
My example unfortunately does not show this why i am wondering what I am missing.
‎2009 Mar 24 10:28 AM
Hi Baran,
Using inner join for tab2 and tab3 is restricting with the less value table tab3.
Try to use left join twice, if your intention is set the priority to tab1.
Kind Regards,
Fernando Da Ró
‎2009 Mar 24 10:11 AM
Hi,
Select a~fld1 a~fld2 a~fld3 a~fld4
from ztab1 as a left outer join ztab2 as b
on a~fld1 = b!fld1
and a~fld2=b~fld2
and a~fld3 = b~fld3
inner join ztab3 as c
on b~fld1 = c~fld1
and b~fld2 = c~fld3
into table itab.
‎2009 Mar 24 10:17 AM
Hi,
Try the following which will work fine:
Select afield from ztab1 as a left outer join ztab2 as b on afield = b~field
inner join ztab3 as c on bfield = cfield into table itab.
best regards,
suresh