结合相同的数据到一行
问题描述:
我已经搜查了很多次,但我无法找到正确的查询,以适应什么,我试图做的。在示例图像中,我想将所有匹配的数据放入一行,并使用Apex Interactive Report应用程序中的pl/sql将推荐列下的数字与逗号相结合。
看起来像这样:
0177 || Martinez的,梅尔乔|| 24-OCT-13 || || 1 17 || || 1,2,8
0178 || Saxon,Victoria || 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
Oracle或sql server? –
为什么使用pl/sql?还是你的意思是使用Oracle SQL?这里不需要pl/sql。另外,Apex Interactive Report有什么特别之处?我假设“没有”,但请澄清。 – mathguy
你能提供一个带有列名的表数据的例子吗?您是否想要将交叉报告中的一行中的一列的结果合并到一列中? –