WITH t(idx, title) AS ( SELECT 1 idx, 'aaa' title UNION ALL SELECT 2, 'ddd' UNION ALL SELECT 3, 'aaa' UNION ALL SELECT 4, 'aaa' ) SELECT title , STUFF((SELECT CONCAT(' | ', s.idx) FROM t s WHERE s.title = m.title ORDER BY s.idx FOR XML PATH('') ), 1, 3, '') idxs FROM t m GROUP BY title ; -- http://www.gurubee.net/article/55512