简体   繁体   English

使用SQL查询整合XML数据

[英]Consolidate XML data using SQL query

I am fairly new to XML and I have been asked to create a data file in XML format using SQL. 我刚接触XML,有人要求我使用SQL创建XML格式的数据文件。 I need to query multiple tables in the database to get all the necessary information and need to consolidate the information in the XML output. 我需要查询数据库中的多个表以获取所有必要的信息,并需要将这些信息合并到XML输出中。 I am struggling with how to consolidate the data in the expected format. 我正在努力如何以预期的格式合并数据。 Any and all suggestions/help is greatly appreciated. 任何和所有建议/帮助将不胜感激。 Thank you! 谢谢! This is the query I am testing with- 这是我正在测试的查询-

(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

My output is like this- 我的输出是这样的-

<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>

The output needs to be consolidated like this- 需要像这样合并输出-

<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>

Next time you ask a question please add some sample data, best with DDL and DML ( read about MCVE ). 下次您提出问题时,请添加一些示例数据,最好使用DDL和DML( 了解有关MCVE的信息 )。 And try to reduce youe part to the relevant pieces. 并尝试将您的部分减少到相关部分。 If there might be more than one: 2 is enough. 如果可能不止一个:2就足够了。 If there are several of the same : 1 is enough. 如果有多个相同 :1就足够了。

Your XML seems to be quite straight. 您的XML似乎很简单。 Just one single row, some elements deeper nested, but still 1:1 . 仅一行,一些元素嵌套得更深,但仍为1:1 The only 1:n element seems to be the <AnesthesiaStaff> . 唯一的1:n元素似乎是<AnesthesiaStaff> Your own query is pretty close actually. 您自己的查询实际上非常接近。

What I changed: 我改变了什么:

  • took away the INNER JOIN provcode_t and placed this within the sub-select ( correlated sub-query ). INNER JOIN provcode_tINNER JOIN provcode_t并将其放在子选择( 相关子查询 )中。
  • Changed 2018-02-05 to 20180205 as the ISO8601 format is not culture independant with DATETIME (don't now your target data type though) 改为2018-02-0520180205为ISO8601格式与文化独立的DATETIME (现在未将目标数据虽然类型)
  • Placed the AnesthesiaStaffSet as ROOT for the sub-select and used [*] as the column name for the created inner XML AnesthesiaStaffSetROOT作为子选择,并使用[*]作为创建的内部XML的列名

Try it out (I cannot test it) 试试看(我无法测试)

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

If this does not solve your issue, please use the edit option to add an MCVE to your question. 如果这不能解决您的问题,请使用编辑选项将MCVE添加到您的问题中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM