APPLICANT_ID DATE_OF_BIRTH
206209579 04/29/82
206209579 04/29/82
203276426 06/01/69
203276426 02/03/96
203276426 06/02/99
我需要结果
206209579 04/29/82,04/29/82
203276426 06/01/69,02/03/96
请提出它的需要
APPLICANT_ID DATE_OF_BIRTH
206209579 04/29/82
206209579 04/29/82
203276426 06/01/69
203276426 02/03/96
203276426 06/02/99
我需要结果
206209579 04/29/82,04/29/82
203276426 06/01/69,02/03/96
请提出它的需要
在 Oracle 11g 及更高版本中,您可以使用listagg
:
SELECT applicant_id,
LISTAGG(date_of_birth) WITHIN GROUP (ORDER BY date_of_birth)
FROM my_table
GROUP BY applicant_id
作为替代方案,对于不支持 11g 之前的 Oracle 版本,您可以使用ROW_NUMBER ()和SYS_CONNECT_BY_PATH函数。LISTAGG
测试用例:
SQL> SELECT * FROM table1;
APPLICANT_ID DATE_OF_B
------------ ---------
206209579 29-APR-82
206209579 29-APR-82
203276426 01-JUN-69
203276426 03-FEB-96
203276426 02-JUN-99
SQL>
SQL> SELECT applicant_id,
2 LTRIM(MAX(SYS_CONNECT_BY_PATH(date_of_birth,','))
3 KEEP (DENSE_RANK LAST ORDER BY curr),',') AS employees
4 FROM (SELECT applicant_id,
5 date_of_birth,
6 ROW_NUMBER() OVER (PARTITION BY applicant_id ORDER BY date_of_birth) AS curr,
7 ROW_NUMBER() OVER (PARTITION BY applicant_id ORDER BY date_of_birth) -1 AS prev
8 FROM table1)
9 GROUP BY applicant_id
10 CONNECT BY prev = PRIOR curr AND applicant_id = PRIOR applicant_id
11 START WITH curr = 1;
APPLICANT_ID EMPLOYEES
------------ --------------------------------------------------
203276426 01-JUN-69,03-FEB-96,02-JUN-99
206209579 29-APR-82,29-APR-82
SQL>
Tim 的文章在这里演示了其他方法。