欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SQL server 和 Oracle 中列转行的小操作

程序员文章站 2022-06-05 08:06:27
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 为关连字段