我对XML很新,我被要求使用SQL创建XML格式的数据文件。我需要查询数据库中的多个表以获取所有必要的信息,并需要合并XML输出中的信息。我正在努力解决如何以预期的格式整合数据。任何和所有建议/帮助非常感谢。谢谢!这是我正在测试的查询 -
(select
p.acctno as [Demographic/PatientID],
p.patdob as [Demographic/DOB],
c.poscode as [Procedure/Facility],
(select
CAST(c.acctno as varchar(10)) +
CONVERT(varchar(10), c.dateofservice, 112) as [AnesthesiaRecordID],
(select
d.provcode as [AnesthesiaStaff/ProvCode]
FOR XML PATH ('AnesthesiaStaffSet'), TYPE
)
FOR XML PATH ('AnethesiaCase'), TYPE
)
from patdemo_t p
inner join charge_t c on p.acctno = c.acctno
inner join provcode_t d on c.provcode = d.provcode
where
p.acctno in (12345, 54321)
and c.dateofservice in ('2018-02-15','2018-02-01')
group by p.acctno, c.poscode, p.patdob, p.patfname,
p.patlname, c.acctno, c.dateofservice, d.provcode
)
FOR XML PATH ('AnesthesiaRecord'), ROOT('AnesthesiaRecords'), TYPE
我的输出是这样的 -
<AnesthesiaRecords>
<AnesthesiaRecord>
<Demographic>
<PatientID>12345</PatientID>
<DOB>1972-03-30T00:00:00</DOB>
</Demographic>
<Procedure>
<Facility>LHOP </Facility>
</Procedure>
<AnethesiaCase>
<AnesthesiaRecordID>1234520180201</AnesthesiaRecordID>
<AnesthesiaStaffSet>
<AnesthesiaStaff>
<ProvCode>BUS</ProvCode>
</AnesthesiaStaff>
</AnesthesiaStaffSet>
</AnethesiaCase>
</AnesthesiaRecord>
<AnesthesiaRecord>
<Demographic>
<PatientID>12345</PatientID>
<DOB>1972-03-30T00:00:00</DOB>
</Demographic>
<Procedure>
<Facility>LHOP </Facility>
</Procedure>
<AnethesiaCase>
<AnesthesiaRecordID>1234520180201</AnesthesiaRecordID>
<AnesthesiaStaffSet>
<AnesthesiaStaff>
<ProvCode>OBR</ProvCode>
</AnesthesiaStaff>
</AnesthesiaStaffSet>
</AnethesiaCase>
</AnesthesiaRecord>
<AnesthesiaRecord>
<Demographic>
<PatientID>54321</PatientID>
<DOB>2006-04-01T00:00:00</DOB>
</Demographic>
<Procedure>
<Facility>LHOP </Facility>
</Procedure>
<AnethesiaCase>
<AnesthesiaRecordID>5432120180215</AnesthesiaRecordID>
<AnesthesiaStaffSet>
<AnesthesiaStaff>
<ProvCode>LUN</ProvCode>
</AnesthesiaStaff>
</AnesthesiaStaffSet>
</AnethesiaCase>
</AnesthesiaRecord>
<AnesthesiaRecord>
<Demographic>
<PatientID>54321</PatientID>
<DOB>2006-04-01T00:00:00</DOB>
</Demographic>
<Procedure>
<Facility>LHOP </Facility>
</Procedure>
<AnethesiaCase>
<AnesthesiaRecordID>5432120180215</AnesthesiaRecordID>
<AnesthesiaStaffSet>
<AnesthesiaStaff>
<ProvCode>REA</ProvCode>
</AnesthesiaStaff>
</AnesthesiaStaffSet>
</AnethesiaCase>
</AnesthesiaRecord>
</AnesthesiaRecords>
输出需要像这样整合 -
<AnesthesiaRecords>
<AnesthesiaRecord>
<Demographic>
<PatientID>12345</PatientID>
<DOB>1972-03-30T00:00:00</DOB>
</Demographic>
<Procedure>
<Facility>LHOP </Facility>
</Procedure>
<AnethesiaCase>
<AnesthesiaRecordID>1234520180201</AnesthesiaRecordID>
<AnesthesiaStaffSet>
<AnesthesiaStaff>
<ProvCode>BUS</ProvCode>
</AnesthesiaStaff>
<AnesthesiaStaff>
<ProvCode>OBR</ProvCode>
</AnesthesiaStaff>
</AnesthesiaStaffSet>
</AnethesiaCase>
</AnesthesiaRecord>
<AnesthesiaRecord>
<Demographic>
<PatientID>54321</PatientID>
<DOB>2006-04-01T00:00:00</DOB>
</Demographic>
<Procedure>
<Facility>LHOP </Facility>
</Procedure>
<AnethesiaCase>
<AnesthesiaRecordID>5432120180215</AnesthesiaRecordID>
<AnesthesiaStaffSet>
<AnesthesiaStaff>
<ProvCode>LUN</ProvCode>
</AnesthesiaStaff>
<AnesthesiaStaff>
<ProvCode>REA</ProvCode>
</AnesthesiaStaff>
</AnesthesiaStaffSet>
</AnethesiaCase>
</AnesthesiaRecord>
</AnesthesiaRecords>
答案 0 :(得分:0)
下次提出问题时,请添加一些示例数据,最好使用DDL和DML(read about MCVE)。并尝试减少相关部分的部分。如果可能有多个:2就足够了。如果有几个相同的:1就够了。
您的XML似乎非常直接。只有一行,一些元素更深层次嵌套,但仍然是1:1
。唯一的1:n
元素似乎是<AnesthesiaStaff>
。你自己的查询实际上非常接近。
我改变了什么:
INNER JOIN provcode_t
并将其放在子选项(相关子查询)中。2018-02-05
更改为20180205
,因为ISO8601格式与DATETIME
无法独立于文化(不过现在不是您的目标数据类型)AnesthesiaStaffSet
放置为ROOT
作为子选择,并将[*]
用作创建的内部XML的列名称 尝试一下(我无法测试)
select
p.acctno as [Demographic/PatientID],
p.patdob as [Demographic/DOB],
c.poscode as [Procedure/Facility],
(select
CAST(c.acctno as varchar(10)) +
CONVERT(varchar(10), c.dateofservice, 112) as [AnesthesiaRecordID],
(select
d.provcode as [AnesthesiaStaff]
from provcode_t d
where c.provcode = d.provcode
FOR XML PATH ('AnesthesiaStaff'),ROOT('AnesthesiaStaffSet'), TYPE
)
FOR XML PATH ('AnethesiaCase'), TYPE
) AS [*]
from patdemo_t p
inner join charge_t c on p.acctno = c.acctno
where
p.acctno in (12345, 54321)
and c.dateofservice in ('20180215','20180201')
group by p.acctno, c.poscode, p.patdob, p.patfname,
p.patlname, c.acctno, c.dateofservice
FOR XML PATH ('AnesthesiaRecord'), ROOT('AnesthesiaRecords'), TYPE
如果这不能解决您的问题,请使用编辑选项为您的问题添加MCVE。