sql group by having count distinct