我知道这是一个老问题,但有一个更简单的解决方案,可以用一个纯 SQL 查询来表达......假设它有效(看到一些 SQL 功能在 ABAP Open SQL 中失败)......
无论如何,要获得您的过滤条件,您需要做的就是按用于查找重复项(在本例中为ALTKN
, BUKRS
)的键进行分组,并且只选择count ( * )
(组中的记录数)高于 1 的组。
接下来使用您的过滤条件来获得您需要的实际结果。
这是一个完整的查询,可以解决这种特殊情况下的问题:
Select kunnr from kna1 as outer_kna1
where exists (
"what you select here doesn't matter
Select 1 from kna1 inner join knb1 on kna1~kunnr = knb1~kunnr
where kna1~kunnr = outer_kna1~kunnr
group by ALTKN, BUKRS "Keys, by which duplicates are searched for
having count( * ) > 1 "Condition to only select duplicates (in this case, record count in a group is more than 1)
)
into table @lt_duplicate_account_data.
lt_kunnrs_of_duplicate_accounts
将包含 customer kunnrs
,但如果您也需要ALTKN
and BUKRS
,只需在主查询中添加一个联接并在子查询中添加相应的条件。
Select kunnr, altkn, bukrs from kna1 as outer_kna1
inner join outer_knb1 on outer_kna1~kunnr = outer_knb1~kunnr "added join
where exists (
"what you select here doesn't matter
Select kna1~kunnr from kna1 inner join knb1 on kna1~kunnr = knb1~kunnr
where kna1~kunnr = outer_kna1~kunnr
and altkn = outer_knb1~altkn and bukrs = outer_knb1~bukrs "added conditions
group by ALTKN, BUKRS "Keys, by which duplicates are searched for
having count( * ) > 1 "Condition to only select duplicates (in this case, record count in a group is more than 1)
)
into table @lt_duplicate_account_data.