postgres=# with x as (select ride, avg(weight), count(distinct owner) o, max(distinct owner) mo, count(distinct wheels) w, max(distinct wheels) mw from rides group by ride) select ride, avg, case when o=1 then mo else null end as owner, case when w=1 then mw else null end as wheels from x ;
ride | avg | owner | wheels
------------+----------------------+-------+--------
bicycle | 25.4000000000000000 | john | 2
motobike | 157.0666666666666667 | adam |
scateboard | 3.6800000000000000 | | 4
(3 rows)
postgres=#