我想知道如果一个字段有一个特定的值是否可以求和,否则只取 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 |
+-----------+------------+--------+