通过在 Snowflake SQL 中按条件进行子聚合来创建列

Creating columns by subaggregating by condition in Snowflake SQL

我关注table:

id1 | id2  | n_products | daydiff
 a  |   1  |       12   |    12
 a  |   1  |       11   |    13
 a  |   1  |       90   |    46
 a  |   2  |       5    |    5
 b  |   2  |       15   |    15
 b  |   2  |       15   |    21
 c  |   3  |       90   |    7

我需要通过 id 聚合此 table 并按以下方式使用 daydiff

这应该使用平均值进行汇总。

结果应该是:

id1 |    id2  |  sub 14 | 14_28 | 28+
 a  |     1   |    11.5 |    0  |  46
 a  |     2   |    5    |    0  |  0
 b  |     2   |    0    |    15 |  0
 a  |     3   |    7    |    0  |  0

我怎样才能做到这一点?我想这会涉及一些 group by 语句,但我不确定应该如何应用它们

使用条件聚合:

select id1, id2,
       avg(case when datediff < 14 then n_products end) as avg_lt14,
       avg(case when datediff >= 14 and datediff <= 28 then n_products end) as avg_14_28,
       avg(case when datediff > 29 then n_products end) as avg_29pl
from t
group by id1, id2;

一些数据库将整数的平均值计算为整数。我不知道雪花是否这样做。如果是,则将 n_products 更改为 n_products * 1.0

Gordon 的答案是跨平台正确的,但对我自己来说,我更喜欢雪花 IFF 语法

SELECT id1, id2,
    AVG(IFF(datediff < 14, n_products, NULL)) as avg_lt14,
    AVG(IFF(datediff >= 14 and datediff <= 28, n_products, NULL)) as avg_14_28,
    AVG(IFF(datediff > 29, n_products, NULL)) as avg_29pl
FROM t
GROUP BY id1, id2;