Select all columns with GROUP BY one column [duplicate] Select all columns with GROUP BY one column [duplicate] postgresql postgresql

Select all columns with GROUP BY one column [duplicate]


A query that works for all DB engines would be

select t1.*from sch.mytable t1join(    SELECT min(id) as id    FROM sch.mytable     GROUP BY key) t2 on t1.id = t2.id

where min(id) is the function that influences which result you get. If you use max(id) you get the other.


distinct on

select distinct on (key) *from torder by key, name

Notice that the order by clause determines which row will win the ties.