如何在Snowflake中创建聚合UDF

时间:2019-04-16 13:40:27

标签: aggregate-functions user-defined-functions snowflake-datawarehouse

我正在尝试创建聚合的UDF,例如类似summedian的东西。

https://docs.snowflake.net/manuals/sql-reference/udf-sql.htmlhttps://docs.snowflake.net/manuals/sql-reference/sql/create-function.html上的文档和示例没有说明如何这样做。

有人可以解释如何和/或提供MWE吗?

1 个答案:

答案 0 :(得分:0)

you want to use a javascript user defined table function of which that have a sum example here

The main gotcha's you want to look out for, is inside the UDF code the SQL parameters names are uppercase, you can see this in the examples, but if you missed noting it, it can lead to lots of head banging. Also Javascript has no int types so, all value have to go in/out via a double, but an int32 can safely be stored in a double, so that is not a major concern.. and if you need more precision, you might want to make you function output not the final "sum" but return multiple values one of which is the an aggregate key, and then sum in SQL space.