gpt4 book ai didi

postgresql - 在 PostgreSQL 中分组时如何做三元运算符?

转载 作者:行者123 更新时间:2023-11-29 14:17:02 39 4
gpt4 key购买 nike

我有一个看起来像这样的表:

| name | feature |
|------|---------|
| bob | t |
| bob | f |

name 是一个文本列,feature 是一个 bool 值。我想运行一个查询,以便如果一个名称在其相应的特征值中有 t,则该名称被分配为 t,否则为 f。有点像

SELECT name, CASE WHEN 't' in ARRAY_AGG(feature) THEN 't' ELSE 'f' END AS custom_feature 
FROM table
GROUP BY name

因此产生:

| name | custom_feature |
|------|----------------|
| bob | t |

在 Postgres 中执行此操作的最佳方法是什么?

最佳答案

有 bool 聚合bool_and() and bool_or() .

你可以这样写你的查询:

SELECT name, bool_or(feature) AS custom_feature 
FROM my_table
GROUP BY name;

关于postgresql - 在 PostgreSQL 中分组时如何做三元运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44531994/

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