你能告诉我为什么在下面选择 LEFT join 作为 INNER join 工作吗?
SELECT FROM @it_selected_data AS selected
LEFT JOIN zkrh_scmt_invh AS invh
ON invh~ebeln = selected~ebeln
FIELDS selected~*, invh~invkind
WHERE invh~invkind = ( SELECT MAX( invkind ) FROM zkrh_scmt_invh WHERE ebeln = invh~ebeln )
INTO CORRESPONDING FIELDS OF TABLE @it_final_data.
提前致谢
埃利亚斯
以下是我在 SAP 论坛中对同一问题的回答。
问题和答案已经在这里、这里等很多地方给出了,但让我用我的话再解释一下:
WHERE
子句中进行过滤。WHERE
条件将失败。ON
子句。在ABAP SQL中,不接受在ON子句中包含复杂的函数(例如ABAP 7.58中的
= ( SELECT ... )
),替代方法是将其保留在WHERE
子句中并指示OR ... IS NULL
:
SELECT FROM @it_selected_data AS selected
LEFT JOIN zkrh_scmt_invh AS invh
ON invh~ebeln = selected~ebeln
FIELDS selected~*, invh~invkind
WHERE ( invh~invkind IS NULL
OR invh~invkind = ( SELECT MAX( invkind )
FROM zkrh_scmt_invh
WHERE ebeln = invh~ebeln ) )
INTO CORRESPONDING FIELDS OF TABLE @it_final_data.
另一个测试,任何人都可以重现(
DE
存在于表T005T
中,但不存在ZY
):
TYPES tt_land1 TYPE STANDARD TABLE OF t005-land1 WITH EMPTY KEY.
TYPES: BEGIN OF ts_country_code_and_name,
land1 TYPE t005t-land1,
landx TYPE t005t-landx,
END OF ts_country_code_and_name.
TYPES tt_country_codes_and_names TYPE STANDARD TABLE OF ts_country_code_and_name WITH EMPTY KEY.
DATA(countries) = VALUE tt_land1( ( 'DE' ) ( 'ZY' ) ).
DATA(country_codes_and_names) = VALUE tt_country_codes_and_names( ).
SELECT
FROM @countries AS countries
LEFT JOIN t005t
ON t005t~land1 = countries~table_line
AND t005t~spras = 'E'
FIELDS countries~table_line AS land1, t005t~landx
WHERE ( t005t~LAND1 IS NULL
OR t005t~LAND1 IN ( SELECT land1 from t005 WHERE land1 LIKE 'D%' ) )
INTO TABLE @country_codes_and_names.
ASSERT country_codes_and_names = VALUE tt_country_codes_and_names(
( land1 = 'DE' landx = 'Germany' )
( land1 = 'ZY' landx = '' ) ).
在互联网上搜索后,我在这里找到了答案
左连接限制
所以我使用CTE如下
WITH
+invkind AS ( SELECT ebeln, MAX( invkind ) AS invkind FROM zkrh_scmt_invh GROUP BY ebeln )
SELECT FROM @it_selected_data AS selected
LEFT JOIN +invkind AS invh
ON invh~ebeln = selected~ebeln
LEFT JOIN dd07t
ON invh~invkind = dd07t~domvalue_l AND
dd07t~domname = 'ZTEST' AND
dd07t~ddlanguage = @sy-langu
FIELDS selected~*, concat_with_space( invh~invkind, dd07t~ddtext, 2 ) AS invkind
INTO CORRESPONDING FIELDS OF TABLE @it_final_data.
非常感谢
埃利亚斯