Finally figured out sql group by