There is nice extension in postgres: fuzzystrmatch I have used to calculate the distance. From documetation: SELECT levenshtein_less_equal('extensive', 'exhaustive',2); You can use it then with your group by query.
pgsql-general by date:
Соглашаюсь с условиями обработки персональных данных