-1

The PG table looks like this:

id - name   - type
1  - Name 1 - Type A
2  - Name 1 - Type B
3  - Name 2 - Type A
4  - Name 2 - Type B
5  - Name 3 - Type A

I would like to write a query that only lists rows in which Name has a 'Type A' record but not a Type B record.

This is the result I am hoping for:

5  - Name 3 - Type A
Andronicus
  • 25,419
  • 17
  • 47
  • 88
Brodie
  • 3,526
  • 8
  • 42
  • 61

2 Answers2

1

You can use a nested select:

select t.*
from table_name t
where not exists(
    select 1
    from table_name it
    where t.name = it.name
    and it.type = 'Type B'
)
and t.type = 'Type A'
Andronicus
  • 25,419
  • 17
  • 47
  • 88
0

One method is group by:

select name
from t
group by t
having min(type) = max(type) and min(type) = 'Type A';
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786