gpt4 book ai didi

postgresql - sql : how to select a row with a true value from a column of boolean values after the HAVING clause

转载 作者:行者123 更新时间:2023-11-29 12:48:09 25 4
gpt4 key购买 nike

HI 有 3 个产品表,每个表有 3 列,即客户名称、 bool 选择退出和黑名单。在 Having 子句之后,每个客户名称将有 3 行(假设他拥有所有 3 种产品)。

如果任何 bool 列包含 true,我如何输出 true。我通过使用下面的 cast 操作想通了,但认为应该有一个更优雅的解决方案。

SELECT customer_name,
cast(int4(sum(cast(optout As int4))) As Boolean) As optout,
cast(int4(sum(cast(blacklist As int4))) As Boolean) As blacklist
FROM
(SELECT * FROM product1
UNION SELECT * FROM product2
UNION SELECT * FROM product3) AS temp1
GROUP BY customer_name, optout, blacklist
HAVING optout=true or blacklist=true;

最佳答案

试试 bool_or聚合函数,听起来正是您要找的:

SELECT customer_name,
bool_or(optout) As optout,
bool_or(blacklist) As blacklist
FROM
(SELECT * FROM product1
UNION SELECT * FROM product2
UNION SELECT * FROM product3) AS temp1
GROUP BY customer_name, optout, blacklist
HAVING optout=true or blacklist=true;

关于postgresql - sql : how to select a row with a true value from a column of boolean values after the HAVING clause,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5700269/

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