sql:如何从 HAVING 子句后的布尔值列中选择具有真值的行
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
bool_or
聚合函数,听起来正是您正在寻找的:Try the
bool_or
aggregate function, sounds like exactly what you're looking for:如果我正确理解了这个问题,我认为你只需要在 SELECT 中使用 CASE 语句,例如
If I have understood the question correctly I think you just need a CASE statement in the SELECT e.g.