如何获得 Snowflake 的累积乘积?

发布于 2025-01-17 19:34:56 字数 100 浏览 3 评论 0原文

我想在雪花中计算跨行的累积产品。

基本上,我每月的费率在整个时间内累积。

(某些数据库为此具有product() SQL函数)。

I want to calculate the cumulative product across rows in Snowflake.

Basically I have monthly rates that multiplied accumulate across time.

(Some databases have the product() SQL function for that).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满身野味 2025-01-24 19:34:56

Sterling Paramore 建议的技巧:添加日志,然后对其求幂:

with data as (select $1 x from values (1),(2),(3),(4),(5))

select x
    , sum(x) over(order by x) sum
    , exp(sum(ln(x)) over(order by x)) mult
from data

在此处输入图像描述

A trick suggested by Sterling Paramore: Add logs, and then exponentiate it:

with data as (select $1 x from values (1),(2),(3),(4),(5))

select x
    , sum(x) over(order by x) sum
    , exp(sum(ln(x)) over(order by x)) mult
from data

enter image description here

踏雪无痕 2025-01-24 19:34:56

如果内置函数不存在,通常可以使用 用户定义的表函数

在本例中:

CREATE OR REPLACE FUNCTION CUMULATIVE_PRODUCT(VALUE double)
    RETURNS TABLE (PRODUCT double)
    LANGUAGE JAVASCRIPT
    AS '{
        initialize: function(argumentInfo, context) {
           this.cumulativeProduct = 1;
        },
        processRow: function f(row, rowWriter, context){
           this.cumulativeProduct = this.cumulativeProduct*row.VALUE;
           rowWriter.writeRow({PRODUCT: this.cumulativeProduct});
        }
    }';

示例表:

create temp table sample_numbers as (
select 1 as index, 5.1::double as current_value
    union all
select 2 as index, 4.3::double as current_value
    union all
select 3 as index, 3.7::double as current_value
    union all
select 4 as index, 3.9::double as current_value
)

调用 UDTF:

select index,current_value,PRODUCT as cumulative_product
from sample_numbers,table(CUMULATIVE_PRODUCT(current_value) over ())

在此处输入图像描述

注意 empty over() 子句 强制 Snowflake 对数据执行单次顺序运行,而不是将其拆分为并行块

If a built-in function doesn't exist, it's usually possible to roll something custom using a User-Defined Table Function.

In this case:

CREATE OR REPLACE FUNCTION CUMULATIVE_PRODUCT(VALUE double)
    RETURNS TABLE (PRODUCT double)
    LANGUAGE JAVASCRIPT
    AS '{
        initialize: function(argumentInfo, context) {
           this.cumulativeProduct = 1;
        },
        processRow: function f(row, rowWriter, context){
           this.cumulativeProduct = this.cumulativeProduct*row.VALUE;
           rowWriter.writeRow({PRODUCT: this.cumulativeProduct});
        }
    }';

Example table:

create temp table sample_numbers as (
select 1 as index, 5.1::double as current_value
    union all
select 2 as index, 4.3::double as current_value
    union all
select 3 as index, 3.7::double as current_value
    union all
select 4 as index, 3.9::double as current_value
)

invoking the UDTF:

select index,current_value,PRODUCT as cumulative_product
from sample_numbers,table(CUMULATIVE_PRODUCT(current_value) over ())

enter image description here

Note the empty over() clause which forces Snowflake to do a single sequential run over the data instead of splitting it into parallel chunks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文