How to Concatenate Multiple Rows' Fields in a Sap Hana Table

SAP HANA SQL - Concatenate multiple result rows for a single column into a single row

I see Kiran has just referred to another valid answer in the comment, but in your example this would work.

SELECT ID, STRING_AGG(Text, ',')
FROM TABLE1
GROUP BY ID;

You can replace the ',' with other characters, maybe a '\n' for a line break

I would caution against the approach to concatenate rows in this way, unless you know your data well. There is no effective limit to the rows and length of the string that you will generate, but HANA will have a limit on string length, so consider that.

Concatenate two columns in HANA database

How about CONCAT twice?

CREATE VIEW "dbo"."ViewSample"
AS
SELECT CONCAT("column1", CONCAT(' / ', "cloumn2")) AS CollectionLabel
FROM "dbo"."T1" T1
CROSS JOIN "dbo"."T2" T2

HANA DB : How to use CONCAT syntax more than 2 fields

You can use the two pipe symbols || for chained concatenation.

Your example would look like this:

CASE 
WHEN T0."U_ISS_SalEmp2" is not null
THEN
T7."SlpName" || '+' ||
T0."U_ISS_SalEmp2" || '+' ||
T0."U_ISS_SalEmp3" || '+' ||
T0."U_ISS_SalEmp4"
ELSE
T7."SlpName"
END AS "Sales Emp"

How do I work with table variables with multiple rows in SAP HANA DB?

The "UNION"-approach is one option to add records to the data that a table variable is pointing to.

Much better than this is to either use arrays to add, remove, and modify data and finally turn the arrays into table variables via the UNNEST function. This is an option that has been available for many years, even with HANA 1.

Alternatively, SAP HANA 2 (starting with SPS 03, I believe), offers additional SQLScript commands, to directly INSERT, UPDATE, and DELETE on table variables. The documentation covers this in "Modifying the Content of Table Variables".
Note, that this feature comes with a slightly different syntax for the DML commands.

As of SAP HANA 2 SPS 04, there is yet another syntax option provided for this:
"SQL DML Statements on Table Variables".
This one, finally, looks like "normal" SQL DML against table variables.

Given these options, the "union"-approach is the last option you should use in your coding.

How to select rows which have same set of values in two columns and hence concatenate the values in the third column?

If you are using SAP HANA then you can use STRING_AGG(Name, ',')

 SELECT po_number, po_item, STRING_AGG(pr_number, ',')
from your_table
group by po_number, po_item;

instead if you are using mysql you can use group_concat

 SELECT po_number, po_item, group_concat(pr_number, ',')
from your_table
group by po_number, po_item;


Related Topics



Leave a reply



Submit