V tomto příspěvku ukáži, jak získat unikátní emaily ze sloupce databáze PostgreSQL seřazené podle domény. K tomu použijeme funkci substring(co, odkud, kam).
select email from table_name order by substring(email, position('@' in email), length(email));
Uvedený select zobrazí záznamy ze sloupce email tabulky table_name a seřadí je dle domény (podle části řetězce, který vrátí substring). Pokud chceme unikátní, select změníme následovně:
select email from (select distinct email from table_name) as sub order by substring(email, position('@' in email), length(email));