聚合函数不会聚合 - 使用求和函数聚合数据集时

Aggregate function will not aggregate - while using sum function to aggregate dataset

我是 snowflake 的新手,我正在尝试编写一个 SQL 查询,该查询将根据 ref 列汇总 2 行。求和函数似乎不起作用,因为我认为我遗漏了一些东西。

样本数据(交易)

| id   | withdraw| pending|   ref   |  Status  |   date   |
|:---- |:-------:| --- --:| -------:|---------:|---------:|
| 100  | 500     | -500   |1234:234 |Confirmed |2022-05-04 |
| 100  | -500    |  500   |1234:234 |Pending   |2022-05-03 |

我想达到的目标

| id   | withdraw| pending|   ref   |  Status  |   date   |
|:---- |:-------:| --- --:| -------:|---------:|---------:|
| 100  | 0       |   0    |1234:234 |Confirmed |2022-05-04 |

这是我目前所做的

select id,
       sum(withdraw),
       sum(pending),
       ref,
       status,
       date,
from transaction
group by id,ref

如果您想要的是 withdrawpending 以及最后 statusdate 每个 idref 的总和然后使用 window 函数:

SELECT DISTINCT 
       id,
       SUM(withdraw) OVER (PARTITION BY id, ref) withdraw,
       SUM(pending) OVER (PARTITION BY id, ref) pending,
       ref,
       FIRST_VALUE(status) OVER (PARTITION BY id, ref ORDER BY date DESC) status,
       MAX(date) OVER (PARTITION BY id, ref) date
FROM transaction;