零值联合函数有什么作用?

发布于 2025-01-29 08:04:40 字数 174 浏览 3 评论 0 原文

我一直在探索DBT工具,并且遇到了以下代码段:

cocece(customer_orders.number_of_orders,0)作为number_of_orders

我知道我知道cocece函数用于返回一个在a中的第一个非零值列表。我不明白的是第二个参数中的零表示什么?

I have been exploring dbt tools and I came across the following code snippet :

coalesce(customer_orders.number_of_orders, 0) as number_of_orders

I understand that a coalesce function is used to return the first non-null value in a list. What I do not understand is what does the zero in the second parameter signify?

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

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

发布评论

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

评论(2

2025-02-05 08:04:40

cocece 可以接受n个参数数。

COALESCE(val1, val2, ...., val_n)

因此,根据查询:

coalesce(customer_orders.number_of_orders, 0) as number_of_orders

如果 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.

COALESCE(val1, val2, ...., val_n)

So according to the query:

coalesce(customer_orders.number_of_orders, 0) as number_of_orders

In case customer_orders.number_of_orders is NULL the result returned in number_of_orders would be 0.

德意的啸 2025-02-05 08:04:40

cocece 可以根据需要使用尽可能多的参数。在大多数情况下(例如在示例中), cocecce(some_column,0)用于防止创建总和或构建平均值不会导致所需的结果。

假设有三列,您想总结它们。如果您不使用 cocece ,则总和将为 null ,即使您的三列中只有一个是 null 。因此,您将使用 cocece 并替换 null 值零,以接收所有而不是null 值的总和。
您可以“翻译” coce 当 struction:

COALESCE(column1,0)

distefter:

CASE WHEN column1 IS NULL THEN 0 ELSE column1 END

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 be NULLeven if only one of your three columns is NULL. So you will use COALESCEand replace NULL values by zero in order to receive the sum of all NOT NULL values.
You can "translate" COALESCE into a CASE WHEN construct:

COALESCE(column1,0)

does following:

CASE WHEN column1 IS NULL THEN 0 ELSE column1 END

Another use case of COALESCE is to replace column1 by column2 if column1 is NULL, if also column2 is NULL, take column3 etc.
I created an example here, so you can see what I mean:
db<>fiddle

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