如何在Teradata中连接字符串

时间:2014-04-13 05:11:43

标签: sql teradata

我将Teradata 14与所有strtok和其他新函数一起使用,但我不允许编写自己的函数。

在下表中,每个人都有很多技能。我该如何连接这些技能?

team    person
1       Mike (swi)
1       Nick (dri)
1       Mike (coo)
2       
3       Kate (swi)
3       Kate (coo)
3       Kate (dri)
3       Wend (fly)
4       Pete (jum)

所需的表是

team    person
1       Mike (swi coo), Nick (dri),
2       
3       Kate (swi coo dri), Wend(fly),
4       Pete (jum),

如何连接字符串?

1 个答案:

答案 0 :(得分:3)

您应该使用递归查询来执行此类操作而不使用UDF。我已经向您提供了聚合技能的查询,使用类似的方法来获得最终结果。

CREATE Volatile Table TempTable1
as
(
SELECT 
team
,substr(person,0,Index(trim(person),'(')) as name
,substr(person,Index(person,'(')+1,3) as skill
,Row_Number() Over(Partition by team,name order by skill) as rnk
from
MainTable)
WITH DATA
Primary Index(team,name)
ON COMMIT Preserve Rows;



CREATE VOLATILE TABLE temp_table2 (team,name)
as
(WITH RECURSIVE temp_table3 (team,name,skill,rnk,lev)
AS
(
SELECT team,name,cast(skill as varchar(1000)),rnk,1 as lev
from TempTable1
where rnk = 1
UNION ALL
SELECT t1.team,t1.name,t1.skill||','||t2.skill,t1.rnk,t2.lev+1 
FROM
TempTable1 t1
Inner join
temp_table3 t2
on t1.team = t2.team
AND t1.name = t2.name
AND t1.rnk = t2.rnk + 1
)
SELECT team,name||'('||skill||')' as new_name
from temp_table3 
qualify rank() over (partition by team,name order by lev desc) = 1)
WITH DATA 
ON COMMIT PRESERVE ROWS;