[英]ABAP CDS aggregated field to use SUM or MIN based on a condition
I would like to know if it's possible to SUM if a field has a specific value otherwise take only the MIN (just one value of the aggregated records)我想知道如果一个字段具有特定值,是否可以求和,否则只取 MIN(聚合记录的一个值)
I tried something like that but it's syntactically not correct, so I'm looking for an alternative to achieve this:我尝试过类似的东西,但它在语法上不正确,所以我正在寻找一种替代方法来实现这一点:
SELECT FROM ZCDS
FIELDS ZCDS~networkID as networkID,
ZCDS~assignment as assignment,
CASE
WHEN ZCDS~assignment = 'N'
THEN MIN( ZCDS~amount )
ELSE SUM( ZCDS~amount )
END as amount
GROUP BY ZCDS~networkID, ZCDS~assignment
INTO TABLE @DATA(result).
Maybe there is a way to determine if the SUM is already not 0, then use the case to stop adding more amount.也许有一种方法可以确定 SUM 是否已经不为 0,然后使用 case 停止添加更多数量。 But I don't know how to access the intermediate value of the SUM in a CASE, probably it's not possible as well
但我不知道如何在 CASE 中访问 SUM 的中间值,可能也不可能
Expected input:预期输入:
+----+-----------+------------+--------+
| ID | NetworkID | Assignment | Amount |
+----+-----------+------------+--------+
| 1 | D/01 | N | 7 |
| 2 | D/01 | N | 5 |
| 3 | D/01 | U | 15 |
| 4 | D/01 | U | 11 |
| 5 | D/02 | N | 40 |
| 6 | D/02 | N | 42 |
+----+-----------+------------+--------+
Expected output:预期输出:
+-----------+------------+--------+
| NetworkID | Assignment | Amount |
+-----------+------------+--------+
| D/01 | N | 5 |
| D/01 | U | 26 |
| D/02 | N | 40 |
+-----------+------------+--------+
Of course there is a way.当然有办法。 Use
HAVING SUM(ZDS~amount)
after GROUP BY
.在
GROUP BY
之后使用HAVING SUM(ZDS~amount)
。
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).
As you put word CDS into question title then I propose you the CDS solution.当您将单词 CDS 放入问题标题时,我会向您推荐 CDS 解决方案。 I recreated your table structure and can confirm this code works on ABAP 7.50 at least
我重新创建了您的表结构,并且可以确认此代码至少适用于 ABAP 7.50
@AbapCatalog.sqlViewName: 'zsql_assign'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'assignment'
define view ZCDS_ASSIGN as select from zfilter {
key networkid,
key assignment,
case assignment when 'U' then sum( amount )
else min( amount )
end as amount
} group by networkid, assignment;
The output of this query:此查询的输出:
In classical ABAP (OpenSQL) this is still not possible.在经典的 ABAP (OpenSQL) 中,这仍然是不可能的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.