样品表
数字 | DEAL_NUMBER | NAME1 | NAME2 |
---|---|---|---|
1 | T01 | 汤姆 | 杰瑞 |
2 | T02 | 勒布朗 | 史蒂文 |
希望输出如下:
数字 | DEAL_NUMBER | NAME1 | NAME2 | 名称_COM |
---|---|---|---|---|
1 | T01 | 汤姆 | 杰瑞 | 勒布朗·汤姆·史蒂文·杰里 |
2 | T02 | 勒布朗 | 史蒂文 | 勒布朗·汤姆·史蒂文·杰里 |
使用 string_agg 函数可以解决,但不够方便。
CREATE TEMP FUNCTION EXCHANGE_PLACE(STR STRING)
RETURNS STRING
AS
((
SELECT ARRAY_TO_STRING(array_reverse(ARRAY_LIST),' ') FROM (SELECT SPLIT(STR,' ')ARRAY_LIST)
));
WITH TBL_D_CUSTOMER AS
(
SELECT "1" AS NUMBER,"T01" AS DEAL_NUMBER,"TOM" AS NAME1, "JERRY" AS NAME2 UNION ALL
SELECT "2","T01","LEBRON","STEVEN"
)
SELECT
*,
EXCHANGE_PLACE(STRING_AGG(NAME1,' ')OVER(PARTITION BY DEAL_NUMBER)) || ' ' || EXCHANGE_PLACE(STRING_AGG(NAME2,' ')OVER(PARTITION BY DEAL_NUMBER)) AS NAME_COM
FROM TBL_D_CUSTOMER
有什么方法可以做得更好吗?