根据每个案例中的其他列生成一列

Generate a column based on other column within each case

输入:

| case_no | activity | country |
|---------|----------|---------|
|       1 | a1       | A       |
|       1 | a1       | A       |
|       1 | a1       | B       |
|       2 | a2       | C       |
|       2 | a2       | D       |
|       3 | a3       | E       |
|       3 | a3       | E       |

输出一个新列case_countries:

| case_no | activity | country | case_countries |
|---------|----------|---------|----------------|
|       1 | a1       | A       | A,B            |
|       1 | a1       | A       | A,B            |
|       1 | a1       | B       | A,B            |
|       2 | a2       | C       | C,D            |
|       2 | a2       | D       | C,D            |
|       3 | a3       | E       | E              |
|       3 | a3       | E       | E              |

我想生成列 case_countries keeps 个案中每个活动的所有国家/地区。有什么建议么?

ps。我使用 Teradata

它并不像 Tim 建议的那样在 Teradata 中真的很难看,但仍然更复杂:

SELECT
    t1.case_no,
    t1.activity,
    t1.country,
    t2.case_countries
FROM yourTable  t1
INNER JOIN
 (
   SELECT case_no, activity,
      Trim(Trailing ',' FROM XmlAgg(country||',' ORDER BY country) (VARCHAR(200))) AS case_countries
   FROM 
    (
      SELECT DISTINCT case_no, activity, country
      FROM yourTable
    ) t2
   GROUP BY 1,2 
 ) t2
ON t1.case_no = t2.case_no
AND t1.activity = t2.activity;