I need to concat string values row wise with '~' as delimiter. I have the following data:
I need to concat 'Comment' column for each 'id' in the ascending order of 'row_id' with '~' as delimiter.
Expected output is as below:
GROUP_CONCAT
is not an option since its not recognized in my Hive version.
I can use collect_set
or collect_list
, but I won't be able to insert delimiter in between.
Is there any workaround?
Use concat_ws function to concatenate values with ^ as a delimiter. If columns are not string, wrap them with cast as string using shell, this will allow concat_ws work with strings and not-string columns.
You can use || operator to concatenate two or more strings. The result of the operation is always a string. If an operand is a number, it is implicitly converted to string before concatenation. If an operand is NULL, it is treated as an empty string '' in the concatenation.
The Hive Query Language (HiveQL) is a query language for Hive to process and analyze structured data in a Metastore. This chapter explains how to use the SELECT statement with WHERE clause. SELECT statement is used to retrieve the data from a table. WHERE clause works similar to a condition.
The CONCAT_WS() function adds two or more strings together with a separator.
collect_list returns array, not string.
Array can be converted to delimited string using concat_ws.
This will work, with no specific order of comments.
select id
,concat_ws('~',collect_list(comment)) as comments
from mytable
group by id
;
+----+-------------+
| id | comments |
+----+-------------+
| 1 | ABC~PRQ~XYZ |
| 2 | LMN~OPQ |
+----+-------------+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With