SQL server 和 Oracle 中列转行的小操作
程序员文章站
2023-08-26 11:26:37
Oracle: create table zjhis.mz_zdxx_zl as select a.sfsb, wm_concat(a.zdmc) as 诊断 from zjhis.mz_zdxx a group by a.sfsb 直接可以使用 wm_concat 函数 SQl SERVER : ......
Oracle:
create table zjhis.mz_zdxx_zl
as
select a.sfsb,
wm_concat(a.zdmc) as 诊断 from zjhis.mz_zdxx a
group by a.sfsb
直接可以使用 wm_concat 函数
SQl SERVER :
select t.patient_id,
t.times,
[ values ] = stuff((select ',' + icd_name
from sx2017.dbo.mz_visit_diagnose
where patient_id = t.patient_id
and times = t.times
for xml path('')), 1, 1, '')
from sx2017.dbo.mz_visit_diagnose t
group by patient_id, times
patient_id, times 为关连字段
t.times,
[ values ] = stuff((select ',' + icd_name
from sx2017.dbo.mz_visit_diagnose
where patient_id = t.patient_id
and times = t.times
for xml path('')), 1, 1, '')
from sx2017.dbo.mz_visit_diagnose t
group by patient_id, times
patient_id, times 为关连字段