Home > Mobile >  How can I get a cumulative product with Snowflake?
How can I get a cumulative product with Snowflake?

Time:03-29

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).

CodePudding user response:

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

CodePudding user response:

If a built-in function doesn't exist, it's usually possible to roll something custom using a 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

  • Related