更新20180419
Teradata(不确定哪个版本)已添加XMLAGG https://info.teradata.com/htmlpubs/DB_TTU_16_00/index.html#page/Teradata_Data_Types/B035-1140-160K/ssm1472241245699.html这将是比递归更好的选择)
原答案:
Teradata 没有group_concat
/listagg
功能。有几种解决方法。我最喜欢的是使用递归 CTE。它的效率不是很高,但它有很好的文档记录和支持的功能。
在你的情况下:
WITH RECURSIVE recCTE AS
(
SELECT
Animal,
Vaccine_Date,
CAST(min(Vaccine) as VARCHAR(50)) as vaccine_list, --big enough to hold concatenated list
1 as depth, --used to determine the largest/last group_concate (the full group) in the final SELECT
Vaccine
FROM table
GROUP BY 1,2
UNION ALL
SELECT
recCTE.Animal,
recCTE.Vaccine_Date,
recCTE.Vaccine || ',' || table.Vaccine
recCTE.depth + ,
table.Vaccine
FROM recCTE
INNER JOIN table ON
recCTE.Animal = table.Animal AND
recCTE.Vaccine_Date = Table.Vaccine_Date
table.vaccine > recCTE.vaccine
)
--Now select the result with the largest depth for each animal/vaccine_date combo
SELECT * FROM recCTE
QUALIFY ROW_NUMBER() OVER (PARTITION BY animal,vaccine_date ORDER BY depth desc) = 1
您可能需要稍微调整一下(可能在连接之前修剪疫苗值等),但它应该能让您大致了解。您可以查看递归CTE文档在这个链接 http://www.info.teradata.com/htmlpubs/db_ttu_14_00/index.html#page/SQL_Reference/B035_1146_111A/ch01.032.061.html,但是很干燥。如果您不熟悉的话,那里也有很多教程。 Teradata 的递归 CTE 实现也与 T-SQL 和 PostgresSQL 的实现非常相似。
作为另一种选择,您可以查看迄今为止尚未记录的tdstats.udfconcat()
正如知识渊博的@dnoeth 在中所解释的那样这个线程 http://community.teradata.com/t5/Database/UDFCONCAT/m-p/23616在 Teradata 社区网站上。