I want to return records from a table in postgresql that when a distinct value in column 1 has more then one different values in column 2 spread over multiple records.
As a simple example:
table1:
column1 - column2
1 – 1
1 – 2
2 – 1
2 – 1
3 - 3
4 - 5
return:
1 - 1
1 - 2
There is a lot of examples showing distinct (such as this), but I’m looking for non distinct.
select distinct (column2, column1) my_distinct from table1;
3
Answers
You can use a CTE to get all the values of
column1
you’re looking for, then filter the table:Schema (PostgreSQL v15)
Query #1
Alternatively you can create a list of the
column2
values (per column1) and unnest that, which avoids scanning the table a second time.Query #2
View on DB Fiddle
This is a vanilla way to do it. Just an exists to check for records where there is a different value on column2 for the same column1.
Fiddle: https://dbfiddle.uk/Zf9vTS9K
You should use window functions for this, it is likely far more efficient than joins.
Unfortunately,
COUNT(DISTINCT
is not implemented for window functions, so you need to hack it withDENSE_RANK
andMAX
Another option in your specific case is to compare counts with two different partitionings.
db<>fiddle