sql:如何从 HAVING 子句后的布尔值列中选择具有真值的行

发布于 2024-11-02 10:27:08 字数 525 浏览 1 评论 0原文

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

如果任何布尔列包含 true,如何输出 true。我通过使用下面的强制转换操作弄清楚了,但认为应该有一个更优雅的解决方案。

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;

HI have 3 product tables, each with 3 columns namely customer name, and boolean optout and blacklist. After the Having clause, there will be 3 rows for each customer name (assuming he has all 3 products).

How do I output a true if any of the boolean columns contains a true. I figured out by using the cast operation below, but think there should be a more elegant solution.

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;

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

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

发布评论

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

评论(2

慈悲佛祖 2024-11-09 10:27:08

尝试 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;

Try the bool_or aggregate function, sounds like exactly what you're looking for:

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;
中性美 2024-11-09 10:27:08

如果我正确理解了这个问题,我认为你只需要在 SELECT 中使用 CASE 语句,例如

CASE
WHEN blackLIST = TRUE OR optout = TRUE THEN 1
ELSE 0
END

If I have understood the question correctly I think you just need a CASE statement in the SELECT e.g.

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