Is it possible to select rows that are DISTINCT ON
some separate, independent sets of columns?
Suppose I want all the rows which match the following conditions:
- distinct on
(name, birth)
- distinct on
(name, height)
So that, out of the following table, the rows marked with a red cross would not be distinct (with an indication of the failing clause):
name birth height
--------------------------
William 1976 1.82
James 1981 1.68
Mike 1976 1.68
Tom 1967 1.79
William 1976 1.74 ❌ (name, birth)
William 1981 1.82 ❌ (name, height)
Tom 1978 1.92
Mike 1963 1.68 ❌ (name, height)
Tom 1971 1.86
James 1981 1.77 ❌ (name, birth)
Tom 1971 1.89 ❌ (name, birth)
In the above example, if the DISTINCT ON
clause had just been DISTINCT ON (name, birth, height)
, then all the rows would have been considered distinct.
Tried and didn't work:
SELECT DISTINCT ON (name, birth) (name, height) ...
SELECT DISTINCT ON (name, birth), (name, height) ...
SELECT DISTINCT ON ((name, birth), (name, height)) ...
SELECT DISTINCT ON (name, birth) AND (name, height) ...
SELECT DISTINCT ON (name, birth) AND ON (name, height) ...
SELECT DISTINCT ON (name, birth) DISTINCT ON (name, height) ...
SELECT DISTINCT ON (name, birth), DISTINCT ON (name, height) ...