将相同的数据合并为一行

时间:2017-07-27 18:26:26

标签: oracle oracle-apex

Example

我已多次搜索,但找不到符合我要求的正确查询。在示例图像中,我希望将所有匹配的数据放在一行中,并在Apex交互式报表应用程序中使用pl / sql将建议(s)列下的数字与逗号结合使用。

看起来像这样:

0177 || Martinez,Melchor || 24-OCT-13 || 1 || 17 || 1,2,8 ||
0178 || Saxon,Victoria || 16-OCT-13 || 2 || 748 || 4,5 ||

1 个答案:

答案 0 :(得分:1)

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