根据条件使用SUM或MIN的ABAP CDS聚合字段

问题描述 投票:3回答:2

我想知道如果某个字段具有特定值,是否可以求和,否则仅取MIN(仅是聚合记录的一个值)

我尝试过类似的操作,但是从语法上讲这是不正确的,所以我正在寻找一种替代方法来实现此目的:

SELECT FROM ZCDS
FIELDS ZCDS~ID as ID,
       ZCDS~assignment as assignment,
       CASE 
         WHEN ZCDS~assignment = 'N'
         THEN MIN( ZCDS~amount )
         ELSE SUM( ZCDS~amount )
       END as amount
GROUP BY ZCDS~ID, ZCDS~assignment
INTO TABLE @DATA(result).

[也许有一种方法可以确定SUM是否已经不为0,然后使用这种情况停止添加更多的金额。但是我不知道如何在CASE中访问SUM的中间值,可能也不可能

sap abap opensql cds
2个回答
0
投票

当然有办法。在HAVING SUM(ZDS~amount)之后使用GROUP BY

SELECT FROM ZCDS
FIELDS ZCDS~ID as ID,
       ZCDS~assignment as assignment,
       CASE 
         WHEN ZCDS~assignment = 'N'
         THEN MIN( ZCDS~amount )
         ELSE SUM( ZCDS~amount )
       END as amount
GROUP BY ZCDS~ID, ZCDS~assignment
HAVING SUM( ZCDS~amount ) = 0
INTO TABLE @DATA(result).

-1
投票

如果我理解正确,则可以使用COND。示例:

DATA(l_value) = COND #(WHEN assignment = 'N'
THEN MIN( amount )
ELSE SUM( amount ) ).
© www.soinside.com 2019 - 2024. All rights reserved.