I have the following query to retrieve all the records having state = 'error' until the state is something different based on this question.
The current query looks like this
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;
However as you can see below all states are being treated as false, ignoring the bool_and(state='error') condition.
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
I would expect all ok rows to return t when state = 'error' and f when state has something else.
I read that bool_and only takes booleans and integers, but I'm not sure about this given the result of the condition is supposed to be boolean?
Any idea why this is not working and how to make sure it work?
bool_andso it aggregates it over all rows - so you have one state=sent which means bool_and(state='error') is false overall - right?.. try addingpartition by statetoover(