简体   繁体   English

根据 ABAP Open SQL 中的列值仅从数据库表中选择一行

[英]Select only one row from a DB Table depending on a column value in ABAP Open SQL

I am looking for a solution where I want to select only one row from a DB Table depending on a column which like a flag.我正在寻找一种解决方案,在该解决方案中,我只想根据像标志一样的列从数据库表中选择一行。

A sample DB table looks like this:示例数据库表如下所示:

C1 | C2 | C3
-----------------
A1 | N1 | 

A1 | N2 | X

A1 | N3 |

A2 | N21 | X

A2 | N22 |

where C1 and C2 are key fields.其中 C1 和 C2 是关键字段。 In this example, A1 has 3 entries and one of which has a flag true ('X').在此示例中,A1 有 3 个条目,其中一个具有标志 true ('X')。

I want to select either the entry with the flag = 'X' or the minimum of C2 value.我想选择标志 = 'X' 的条目或 C2 值的最小值。

Is this possible in ABAP Open SQL?这在 ABAP Open SQL 中可能吗? I tried using case statement but does not give me the required result.我尝试使用 case 语句,但没有给我所需的结果。

EDIT 1:编辑 1:

In the above example: result will be在上面的例子中:结果将是

A1 | A1 | N2氮气

A2 | A2 | N21 N21

and when the flag is false or empty then:当标志为假或空时:

A1 | A1 | N1 N1

A2 | A2 | N21 N21

Of course it is possible.当然这是可能的。 In fact it should not differ too much from the standard SQL.事实上,它不应该与标准 SQL 有太大区别。

SELECT *
  FROM <your_table>
  WHERE
    c3 = 'X'
    OR
    NOT EXISTS ( SELECT * FROM <your_table> WHERE c3 = 'X' ) 
      AND ( c2 = ( SELECT MIN( c2 ) FROM <your_table> ) )
  INTO TABLE @DATA(lt_your_table).

Here is a sample report done with table T000 .这是使用表T000完成的示例报告。

REPORT yyy.

SELECT *
  FROM t000
  WHERE
    mandt = '101'
    OR
    mandt = ( SELECT MIN( mandt ) FROM t000 )
      AND NOT EXISTS ( SELECT * FROM t000 WHERE mandt = '101' )
  INTO TABLE @DATA(lt_your_table).

LOOP AT lt_your_table ASSIGNING FIELD-SYMBOL(<fs_your_table>).
  WRITE <fs_your_table>-mandt.
ENDLOOP.

EDIT: After your comments the query could look like this.编辑:在您发表评论后,查询可能如下所示。

SELECT mandt, cccoractiv
  FROM t000
  WHERE
    cccopylock = 'X'
UNION
SELECT mandt, MIN( cccoractiv ) AS cccoractiv
  FROM t000
  WHERE
    cccopylock <> 'X'
      AND NOT EXISTS ( SELECT * FROM t000 WHERE cccopylock = 'X' )
    GROUP BY mandt
INTO TABLE @DATA(lt_your_table).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM