Teradata SQL:如何使用 SUM 函数根据情况 A 或 B 将总和分为 2 组?
Teradata SQL: How to use SUM function to Sum total into 2 groups depending on whether case A or B?
SELECT
Account,
CASE WHEN Type = 1 THEN SUM(Amount) END AS Type1_tot,
CASE WHEN Type = 2 THEN SUM(Amount) END AS Type2_tot
FROM Table
Error - Selected non-aggregate values must be part of the associated group.
请在下面查找 table 数据和预期结果:
使用条件聚合,而不是对 CASE
表达式求和:
SELECT
Account,
SUM(CASE WHEN Type = 1 THEN Amount ELSE 0 END) AS Type1_tot,
SUM(CASE WHEN Type = 2 THEN Amount ELSE 0 END) AS Type2_tot
FROM yourTable
GROUP BY
Account
SELECT
Account,
CASE WHEN Type = 1 THEN SUM(Amount) END AS Type1_tot,
CASE WHEN Type = 2 THEN SUM(Amount) END AS Type2_tot
FROM Table
Error - Selected non-aggregate values must be part of the associated group.
请在下面查找 table 数据和预期结果:
使用条件聚合,而不是对 CASE
表达式求和:
SELECT
Account,
SUM(CASE WHEN Type = 1 THEN Amount ELSE 0 END) AS Type1_tot,
SUM(CASE WHEN Type = 2 THEN Amount ELSE 0 END) AS Type2_tot
FROM yourTable
GROUP BY
Account