Fixing postgresql group by