mtmx
mtmx

Reputation: 927

How to select ids which have two different records?

I have table in my db with fields: id, application_id and type_id.

I want to select from this table application_id's which have 2 different records in table (one with type_id=2 and second with type_id=4)

How to do it in PostgreSQL? I can achieve it by group by and having or only with loop?

Upvotes: 1

Views: 42

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269873

You can use aggregation and having:

select application_id
from t
where type_id in (2, 4)
group by application_id
having min(type_id) <> max(type_id);

Upvotes: 2

Related Questions