sql - NOT EXIST in correalted subquery returns too many rows -
i want select records n number of conditions matches associated table.
i try accomplish this
select v.id, name visitors v left join trackings t on t.visitor_id = v.id v.app_id = 'a0i' , ( (not exists ( select v.id trackings not_t v.id = not_t.visitor_id , field = 'admin' )) or (t.field = 'app_name' , t.string_value ilike 'gitchecker') or (t.field = 'users_created' , t.integer_value > 0) ) group v.id having count(*) = 3 -- <number of conditions>
which works fine, unless try express 'unknown' condition through not exists
subquery. subquery returns many rows, not seem filtered on v.id = not_t.visitor_id
any ideas?
i'm not sure why version not working. might having
clause should 2 or 3 depending on match.
but, why not phrase logic this?
select v.id, name visitors v join trackings t on t.visitor_id = v.id v.app_id = 'a0i' group v.id, name having sum( (t.field = 'app_name' , t.string_value ilike 'gitchecker')::int) > 0) , sum( (t.field = 'users_created' , t.integer_value > 0)::int) > 0 , sum( (t.field = 'admin')::int) = 0;
note left join
unnecessary because conditions require matches.
i find these types of queries -- set-with-in-set queries -- group by
, having
versatile way express conditions.
Comments
Post a Comment