gpt4 book ai didi

postgresql - 在 Postgres 中的 bool_and 中将字符串转换为 bool 值或整数

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

我有以下查询来检索所有具有 state = 'error' 的记录,直到状态有所不同 based on this question .

当前查询看起来像这样

SELECT * from (select id, subscription_id, state, created_at,
bool_and(state='error') OVER (order by created_at, id) AS ok
FROM messages ORDER by created_at) m2
WHERE subscription_id = 1;

但是正如您在下面看到的那样,所有状态都被视为 false,忽略了 bool_and(state='error') 条件。

   id    | subscription_id | state |         created_at         | ok 
---------+-----------------+-------+----------------------------+----
69480 | 1 | error | 2015-06-30 15:20:03.041045 | f
69482 | 1 | error | 2015-06-30 15:20:04.582907 | f
69492 | 1 | sent | 2015-06-30 22:50:04.50478 | f
69494 | 1 | error | 2015-06-30 22:50:06.067279 | f
69502 | 1 | error | 2015-07-01 22:50:02.356113 | f

我希望所有 ok 行在 state = 'error' 时返回 t ,在 state 时返回 f还有别的东西。

我读到 bool_and 只接受 bool 值和整数,但鉴于条件的结果应该是 bool 值,我不确定这一点?

知道为什么这不起作用以及如何确保它起作用吗?

最佳答案

您没有为 bool_and 划分聚合,因此它会在所有行上聚合它 - 所以您有一个 state=sent,这意味着 bool_and(state='错误') 为假 整体

尝试将partition by state添加到over(部分

更新 更好的改变

bool_and(state='error') OVER (order by created_at, id) AS ok

bool_and(state='error') OVER (partition by state) AS ok

关于postgresql - 在 Postgres 中的 bool_and 中将字符串转换为 bool 值或整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44518900/

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