Unfortunately, there is no PIVOT in Teradata (only TD_UNPIVOT at 14.10).
If you're lucky, there is a consolidated UDF on your site to make a concat group (perhaps a small opportunity).
Otherwise, there are two options: recursion or aggregation.
If the maximum number of rows per identifier is known, then aggregation is usually faster. This is a lot of code, but most of them are based on cut and paste.
SELECT id, MAX(CASE WHEN rn = 1 THEN string END) || MAX(CASE WHEN rn = 2 THEN ',' || string ELSE '' END) || MAX(CASE WHEN rn = 3 THEN ',' || string ELSE '' END) || MAX(CASE WHEN rn = 4 THEN ',' || string ELSE '' END) || ...
For large tables, this is much more efficient when you materialize the result of a view in a flying table first, using the GROUP BY column as PI.
For recursion, you should also use the Volatile table, since OLAP functions are not allowed in the recursive part. Using a view instead will compute the OLAP function multiple times and therefore lead to poor performance.
CREATE VOLATILE TABLE vt AS ( SELECT id ,string ,ROW_NUMBER() OVER (PARTITION BY id ORDER BY string DESC) AS rn
There is one problem with this approach, it may take a lot of coils, which is easy to see when you omit WHERE rn = 1 .
source share