Search code examples
sqlimpala

Impala - How Stuff and 'For Xml Path' work in Impala SQL


Table is:

+----+------+
| Id | Name |
+----+------+    
| 1  | aaa  |
| 1  | bbb  |
| 1  | ccc  |
| 1  | ddd  |
| 1  | eee  |
+----+------+

Required output:

+----+---------------------+
| Id |        abc          |
+----+---------------------+ 
|  1 | aaa,bbb,ccc,ddd,eee |
+----+---------------------+

Query:

SELECT ID,  abc = STUFF(
         (SELECT ',' + name 
          FROM temp1 t1
          WHERE t1.id = t2.id
          FOR XML PATH (''))
         , 1, 1, '') from temp1 t2
group by id;

We know this will work in SQL Server. But when running in Impala, it will get error AnalysisException: Syntax error in line 54:undefined: FOR XML PATH('')), 1, 1, '').

Does this mean Imapla does not support FOR XML PATH? If no, is there any other way to implement this into Impala?


Solution

  • use group_concat() function:

    Here you'll find the reference

    select id, group_concat(name,',') from tablename
    group by id