零值联合函数有什么作用?
我一直在探索DBT工具,并且遇到了以下代码段:
cocece(customer_orders.number_of_orders,0)作为number_of_orders
我知道我知道cocece函数用于返回一个在a中的第一个非零值列表。我不明白的是第二个参数中的零表示什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
cocece 可以接受n个参数数。
因此,根据查询:
如果
customer_orders.number_of_orders
是null
返回的结果number_of_orders
是0。The COALESCE function returns the first non-null value in a list.
COALESCE
can take n number of arguments.So according to the query:
In case
customer_orders.number_of_orders
isNULL
the result returned innumber_of_orders
would be 0.cocece
可以根据需要使用尽可能多的参数。在大多数情况下(例如在示例中),cocecce(some_column,0)
用于防止创建总和或构建平均值不会导致所需的结果。假设有三列,您想总结它们。如果您不使用
cocece
,则总和将为null
,即使您的三列中只有一个是null
。因此,您将使用cocece
并替换null
值零,以接收所有而不是null
值的总和。您可以“翻译”
coce
当 struction:distefter:
cocecce
的另一种用例时,如果Column1为null
,如果列2为null
,请列列3。我在这里创建了一个示例,因此您可以看到我的意思:
COALESCE
can use as many arguments as you want. In most cases (like in your example),COALESCE(some_column,0)
is used to prevent that creating a sum or building an average will not lead to the desired result.Assume there are three columns and you want to sum them. In case you don't use
COALESCE
, the sum will beNULL
even if only one of your three columns isNULL
. So you will useCOALESCE
and replaceNULL
values by zero in order to receive the sum of allNOT NULL
values.You can "translate"
COALESCE
into aCASE WHEN
construct:does following:
Another use case of
COALESCE
is to replace column1 by column2 if column1 isNULL
, if also column2 isNULL
, take column3 etc.I created an example here, so you can see what I mean:
db<>fiddle