将相同的数据合并为一行

Combine same data into one row

Example

我已经搜索了很多次,但找不到适合我要执行的操作的正确查询。在示例图像中,我希望将所有匹配数据放入一行,并在 Apex Interactive Report 应用程序中使用 pl/sql 将 Recommendation(s) 列下的数字与逗号组合。

看起来像这样:

0177||马丁内斯,梅尔乔||24-OCT-13||1||17||1,2,8||
0178||撒克逊,维多利亚||16-OCT-13||2||748||4,5||

alter session set nls_date_format = 'dd-MON-rr';   -- NOT PART OF THE QUERY

with test_data ( subject_number, patient, mdt_date, pres_phys, cr_mdt_phys, recomm ) as (
  select '0177', 'Martinez, Melchior', to_date ('24-OCT-13'), 1,  17, 1 from dual union all
  select '0177', 'Martinez, Melchior', to_date ('24-OCT-13'), 1,  17, 2 from dual union all
  select '0177', 'Martinez, Melchior', to_date ('24-OCT-13'), 1,  17, 8 from dual union all
  select '0178', 'Saxon, Victoria'   , to_date ('16-OCT-13'), 2, 748, 4 from dual union all
  select '0178', 'Saxon, Victoria'   , to_date ('16-OCT-13'), 2, 748, 5 from dual
)
-- End of simulated inputs (for testing purposes only, not part of the solution).
-- SQL query begins BELOW THIS LINE.
select subject_number patient, mdt_date, pres_phys, cr_mdt_phys,
       listagg(recomm, ',') within group (order by recomm) as recommendations
from   test_data
group by subject_number, patient, mdt_date, pres_phys, cr_mdt_phys
order by subject_number, patient, mdt_date, pres_phys, cr_mdt_phys  --  If needed
;

PATIENT  MDT_DATE   PRES_PHYS  CR_MDT_PHYS  RECOMMENDATIONS
-------  ---------  ---------  -----------  ---------------
0177     24-OCT-13          1           17  1,2,8
0178     16-OCT-13          2          748  4,5