Wednesday, 15 July 2015

sql - Only return results if all rows that match 1st where clause also match the 2nd -



sql - Only return results if all rows that match 1st where clause also match the 2nd -

tables

actions ---------- action_id tasks --------- task_id action_id status_id (can be: 1=waiting, 2=processing, 3=failed) dt_created

i trying write query homecoming 'actions' that:

if have tasks created in lastly 24 hours all have 'status' failed

my difficulty making sure there not task created in lastly 24 hours did not fail.

thus simple query fails homecoming results match both.

select .... tasks.dt_created > tasks.dt_created > () - interval '1 day' , tasks.status_id=3

am sure simple query, must searching using wrong terminology find nothing

select action_id actions inner bring together tasks using(action_id) tasks.dt_created > () - interval '1 day' grouping action_id having bool_and(status_id = 3)

bool_and evaluate

true if input values true, otherwise false

http://www.postgresql.org/docs/current/static/functions-aggregate.html

this version using bool_or might faster

select action_id actions inner bring together tasks using(action_id) tasks.dt_created > () - interval '1 day' grouping action_id having not bool_or(status_id <> 3)

sql postgresql

No comments:

Post a Comment