concat_ws 是 Hive 中的一個字符串聚合函數,它用于將多個字符串值連接在一起,并在每個分隔符的位置插入分隔符
以下是 concat_ws 的基本語法:
concat_ws(string str, string sep, [string...])
參數說明:
str:要連接的字符串列。sep:用作分隔符的字符串。[string...]:可選的額外字符串列,這些列將作為分隔符插入到結果字符串中。以下是一個使用 concat_ws 的示例:
假設我們有一個名為 employees 的表,其中包含以下列:id、first_name、last_name 和 department。我們希望將這些列連接成一個完整的員工姓名,并在每個部分之間插入一個空格。
我們可以使用以下查詢實現這一目標:
SELECT
id,
concat_ws(' ', first_name, last_name) AS full_name,
department
FROM
employees;
這將返回以下結果:
id | full_name | department
-----------------------------
1 | John Doe | HR
2 | Jane Smith | IT
3 | Michael Brown| Sales
在這個例子中,我們使用 concat_ws 函數將 first_name 和 last_name 列連接在一起,并在它們之間插入一個空格。然后,我們將結果與 department 列一起選擇。