gpt4 book ai didi

postgresql - 具有 NULL 值的 bool_and 和 bool_or 的行为

转载 作者:行者123 更新时间:2023-12-04 16:00:17 25 4
gpt4 key购买 nike

我正在使用聚合函数 bool_orbool_and 来聚合一些记录并查看特定列是否存在分歧。根据official documentation :

bool_and(expression)    true if all input values are true, otherwise false
bool_or(expression) true if at least one input value is true, otherwise false

但是这个测试查询:

SELECT bool_or(val),bool_and(val) FROM UNNEST(array[true,NULL]::bool[]) t(val)

两列的结果都为 true

我认为 bool_and 排除了 NULL 值。有什么方法可以使用内置聚合函数使上述查询返回trueNULL

最佳答案

为了我需要做的事情,我最终使用了两个额外的列,您可以在更新的测试查询中看到它们:

SELECT
bool_or(val),
bool_and(val),
bool_or(val IS NULL),
bool_and(val IS NULL)
FROM UNNEST(array[true,NULL]::bool[]) t(val)

这足以涵盖本专栏的所有三个值。

请注意,使用 COUNT(DISTINCT val) 将不起作用,因为不包括 NULL

希望对大家有所帮助!

关于postgresql - 具有 NULL 值的 bool_and 和 bool_or 的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59894718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com