我有这些 table

表1 tbl1_site[facilityId][name]
表2 tbl2_applicant[pvid][facilityId][npi][firstname]
FK连接 key :tbl1_site.facilityId = tbl2_applicant.facilityId

表3 tbl3_abstraction[pvid][patientnumber][diabetesdiagnosis][dateofbirth]
FK连接 key :tbl2_applicant.pvId = tbl3_abstraction.pvId

我在创建一个SQL查询以重现此xml输出时遇到问题。

谢谢

<account>
    <metadata />
    <practice-sites>
        <practice-site>
            <metadata>
                <data-element id="name">
                    <value>My Own Diabetes Medical Center</value>
                </data-element>
            </metadata>
            <applicants>
                <metadata />
                <applicant>
                    <metadata>
                        <data-element id="npi">
                            <value>1234567890</value>
                        </data-element>
                        <data-element id="firstname">
                            <value>Joseph</value>
                        </data-element>
                    </metadata>
                    <clinical-abstractions>
                        <clinical-abstraction>
                            <data-element id="diabetesdiagnosis">
                                <value>Backward</value>
                            </data-element>
                            <data-element id="dateofbirth">
                                <value>02/01/2009</value>
                            </data-element>
                            <data-element id="patientnumber">
                                <value>1</value>
                            </data-element>
                        </clinical-abstraction>
                    </clinical-abstractions>
                </applicant>
            </applicants>
        </practice-site>
    </practice-sites>
</account>

最佳答案

您真的需要所有这些标签吗?
我的意思是“元数据”和“数据元素”

试试这个查询,它以您需要的格式显示数据:

select  t1.name as PracticeSite,
        (SELECT t2.npi as NPI,
                t2.firstname,
                (SELECT t3.patientnumber,
                        t3.diabetesdiagnosis,
                        t3.dateofbirth
                        FROM tbl3_abstraction t3
                        WHERE t3.pvId=t2.pvId
                        FOR XML PATH('clinical-abstraction'), TYPE
                        ) as 'clinical-abstractions'
                FROM tbl2_applicant t2
                WHERE t1.[facilityId]=t2.[facilityId]
                FOR XML PATH('Applicant'), TYPE
        ) AS 'Applicants'
from tbl1_site t1
FOR XML path('PracticeSites'), root('account'), ELEMENTS;

关于.net - SQL查询产生XML输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9135260/

10-11 02:59