sql - Get rows where two values are unique to each other -
the values of interest ein
, registration number reg
.
there lots of records each value.
what want know pair of values appears each other (or blank ein
).
id ein reg 12 321 124 13 321 125 14 322 168 15 322 168 16 323 171 17 323 171 18 323
so in above example, i'd want select 322 , 168, every time appear, appear together, want select 323 , 171, since never appear value, non-value. 100% of records have ein, smaller portion have registration id.
any suggestions on how query this?
one of many possible ways:
select distinct ein, reg test t reg not null , not exists ( select 1 test t1 t1.ein = t.ein , t1.reg <> t.reg or t1.reg = t.reg , t1.ein <> t.ein);
ein | reg ----+----- 323 | 171 322 | 168
- i assuming
ein
not null
,reg
cannull
, since wrote:
100% of records have ein, smaller portion have registration id.
null
never qualifies operator<>
,(323, 171)
not excluded.i exclude rows reg
is null
result, seems intention.
not included.(323, null)