0

样品表

数字 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

有什么方法可以做得更好吗?

4

1 回答 1

2

考虑以下方法

select *, array_to_string(
  array_reverse(array_agg(NAME1) over win) || array_reverse(array_agg(NAME2) over win)
  , ' ') as Name_COM
from TBL_D_CUSTOMER 
window win as (partition by DEAL_NUMBER)      

如果应用于您问题中的样本数据 - 输出是

在此处输入图像描述

于 2021-11-11T03:31:09.680 回答