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
我需要的結果是我需要從select語句導致出身的多重數據單個ID
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
我需要的結果是我需要從select語句導致出身的多重數據單個ID
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
作爲替代方案,用於Oracle版本之前到11g其中LISTAGG
不支持,您可以使用ROW_NUMBER()和SYS_CONNECT_BY_PATH函數。
測試用例:
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>
有添的文章here證明的其他方式。
Downvoter,你甚至會關心告訴downvote的原因嗎?爲了以防萬一,我添加了一個註釋。 – 2015-02-24 14:34:44
非常感謝Lalit Kumar B, – 2015-02-27 05:06:16
@AnandYadav,不客氣。 – 2015-02-27 05:13:14
有多種方法可以做'字符串聚合'。你需要指定你的版本。 11g以後,你可以使用'LISTAGG',否則如果你在之前的版本中,那麼看看我的答案。 – 2015-02-24 14:36:27