You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用单条ABAP SELECT语句筛选BSID表中重复的BUKRS-KUNNR-DMBTR组合

How to Retrieve Duplicate (BUKRS, KUNNR, DMBTR) Combinations in BSID with a Single SELECT Statement

Yep, you absolutely can pull this off with one SELECT statement—your original query was just tripping up on where to place the aggregate filter. Let’s fix that right away.

The Working Query

The key mistake in your initial code was using WHERE to filter the COUNT(*) result. Aggregate conditions like this belong in the HAVING clause: WHERE filters individual rows before grouping happens, while HAVING filters the grouped results after aggregation is done.

Here’s the corrected version:

SELECT bukrs kunnr dmbtr COUNT(*)
  INTO TABLE git_double
  FROM bsid
 WHERE bukrs = '1000'
   AND blart = 'WP'
   AND budat IN s_budat
   AND gjahr IN s_gjahr
 GROUP BY bukrs kunnr dmbtr
HAVING COUNT(*) > 1.

What This Does Step-by-Step

  1. The WHERE clause first narrows down BSID records to only those matching your core criteria (company code 1000, document type WP, date ranges, etc.).
  2. It then groups the remaining records by the three fields you care about: BUKRS, KUNNR, DMBTR.
  3. Finally, the HAVING clause keeps only those groups where the combination appears more than once, along with the count of duplicates for each group.

If You Need Full Duplicate Rows (Not Just Grouped Summaries)

If your goal is to pull every individual record that’s part of a duplicate combination (instead of just the grouped count), you can nest the grouping logic in a subquery:

SELECT bukrs kunnr dmbtr
  INTO TABLE git_double
  FROM bsid
 WHERE bukrs = '1000'
   AND blart = 'WP'
   AND budat IN s_budat
   AND gjahr IN s_gjahr
   AND (bukrs, kunnr, dmbtr) IN (
       SELECT bukrs kunnr dmbtr
         FROM bsid
        WHERE bukrs = '1000'
          AND blart = 'WP'
          AND budat IN s_budat
          AND gjahr IN s_gjahr
        GROUP BY bukrs kunnr dmbtr
       HAVING COUNT(*) > 1
       ).

This will return every row from BSID that belongs to a repeating (BUKRS, KUNNR, DMBTR) set.

内容的提问来源于stack exchange,提问作者ekekakos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:19:43