I need result from select statement as multiple data of birth for a single id

老子叫甜甜 提交于 2019-12-13 16:55:06

问题


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

I need result as

206209579       04/29/82,04/29/82
203276426       06/01/69,02/03/96

please suggest its need full


回答1:


In Oracle 11g and above, you can use listagg:

SELECT   applicant_id,
         LISTAGG(date_of_birth) WITHIN GROUP (ORDER BY date_of_birth)
FROM     my_table
GROUP BY applicant_id 



回答2:


As an alternative, for Oracle version prior to 11g where LISTAGG is not supported, you could use ROW_NUMBER() and SYS_CONNECT_BY_PATH functions.

Test case:

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>

There are other ways demonstrated in Tim's article here.



来源:https://stackoverflow.com/questions/28690375/i-need-result-from-select-statement-as-multiple-data-of-birth-for-a-single-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!