Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group by Concat Teradata

Tags:

sql

teradata

I have a problem with a table, I would like to concat a string field using a group by. I have this situation here:

USER | TEXT
A    | 'hello'
A    | 'by'
B    | 'hi'
B    | '9'
B    | 'city'

I would like to obtain this result:

USER | TEXT
A    | 'hello by'
B    | 'hi 9 city'
like image 861
Davide Morgagni Avatar asked Sep 08 '17 14:09

Davide Morgagni


1 Answers

You can try using xmlagg

SELECT
   User
  ,TRIM(TRAILING ' ' FROM (XMLAGG(TRIM(text)|| ','
                           ORDER BY ColumnPosition) (VARCHAR(1000))))
FROM table
GROUP BY 1
like image 68
SriniV Avatar answered Oct 21 '22 04:10

SriniV