Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate multiple rows' fields in a sap hana table

Tags:

sql

hana

I use SAP-HANA database. I have a simple 2 column table whose columns are id, name. The rows are these:

1 - tom
1 - harry
1 - jack
2 - larry

I would like to group the rows by the id, and concatenate the names into a field, and thus obtain this:

1 - tom, harry, jack
2 - larry

Can you please tell me how we can perform this operation in sap-hana? Thanks in advance.

like image 239
Nishanth Reddy Avatar asked Oct 31 '14 11:10

Nishanth Reddy


1 Answers

If you are using HANA with revision 70, you can try this:-

SELECT ID, STRING_AGG(Name, ',')  AS Names
FROM TAB
GROUP BY ID;

And for more info read this

How to Concatenate Column Value from Multiple Rows into a Single Column?

like image 72
Ankit Bajpai Avatar answered Sep 30 '22 10:09

Ankit Bajpai