3

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

我尝试了类似的方法,但它在语法上不正确,所以我正在寻找一种替代方法来实现这一目标:

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).

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

预期输入:

+----+-----------+------------+--------+
| 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 |
+----+-----------+------------+--------+

预期输出:

+-----------+------------+--------+
| NetworkID | Assignment | Amount |
+-----------+------------+--------+
| D/01      | N          |      5 |
| D/01      | U          |     26 |
| D/02      | N          |     40 |
+-----------+------------+--------+
4

2 回答 2

1

当然有办法。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).
于 2020-03-05T11:07:52.857 回答
0

当您将单词 CDS 放入问题标题时,我建议您使用 CDS 解决方案。我重新创建了您的表结构,并且可以确认此代码至少适用于 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;

此查询的输出:

在此处输入图像描述

在经典的 ABAP (OpenSQL) 中,这仍然是不可能的。

于 2020-04-10T14:32:18.307 回答