Home > Back-end >  Postgres, get distinct field that has value as specified
Postgres, get distinct field that has value as specified

Time:08-13

Please see table below:

id   ticket_id   event
1     130        response
2     130        query
3     130        create
4     130        update
5     131        response
6     131        query
7     131        create
8     132        response
9     132        query
10    132        create

How do i return distinct ticket_id that has no 'update' (value) on event field in a postgres sql

example it should return ticket_id 131 and 132

Thanks.

CodePudding user response:

You can use a NOT EXISTS condition:

select distinct t1.ticket_id
from the_table t1
where not exists (select * 
                  from the_table t2 
                  where t2.ticket_id = t1.ticket_id
                    and t2.event = 'update')
  • Related