SQL到XML输出

时间:2014-02-20 01:42:10

标签: sql sql-server xml tsql

我希望能得到一些帮助,解决我一直在努力解决的一个有点复杂问题的最后一部分。

我们必须从正在生成的SQL表中生成XML文件。

XML的核心需要三个元素。 患者    PhoneAssessment    F2FAssessment

这正如我将在我的测试代码中显示的那样。但是,我们遇到的一个问题是,如果有人同时拥有F2FAssessment和PhoneAssessment,它将生成多个标签。

如果你们都能给我一些有关解决问题的最佳方法的见解,那么只有一个患者标签包含所有可能的PhoneAssessment和F2FAssessment标签,我们将不胜感激。

这是SQL代码:

 use tempdb;
    declare @t table
    (
        [people_id] [nvarchar](255) NULL,
        [actual_date] [date] NULL,
        [NPI] [int] NULL,
        [FileCreationDate] [date] NULL,
        [FileCreationTime] [time](7) NULL,
        [ProviderPatientNo] [int] NULL,
        [LastName] [nvarchar](255) NULL,
        [FirstName] [nvarchar](255) NULL,
        [SSN] [nvarchar](255) NULL,
        [DOB] [date] NULL,
        [Gender] [int] NULL,
        [Race] [int] NULL,
        [Ethnicity] [int] NULL,
        [ProviderPhoneAssessmentId] [nvarchar](255) NULL,
        [CallEndDate] [date] NULL,
        [CallEndTime] [time](7) NULL,
        [DispatchDate] [date] NULL,
        [DispatchTime] [time](7) NULL,
        [CallDisposition] [int] NULL,
        [DispositionOther] [nvarchar](255) NULL,
        [Notes] [nvarchar](255) NULL,
        [ProviderF2FAssessmentId] [nvarchar](255) NULL,
        [AssessmentDate] [date] NULL,
        [ArrivalTime] [time](7) NULL,
        [ResidentialStatus] [int] NULL,
        [County] [int] NULL,
        [EmploymentStatus] [int] NULL,
        [MaritalStatus] [int] NULL,
        [MilitaryStatus] [int] NULL,
        [NumArrests30Days] [nvarchar](255) NULL,
        [AttendedSchoolLast3Months] [int] NULL,
        [EducationLevel] [int] NULL,
        [PrimaryPayorSource] [int] NULL,
        [SecondaryPayorSource] [int] NULL,
        [AnnualHouseholdIncome] [int] NULL,
        [NumberInHousehold] [int] NULL,
        [CurrentServices] [int] NULL,
        [MHTreatmentDeclaration] [int] NULL,
        [MOTStatus] [int] NULL,
        [DurablePOA] [int] NULL,
        [AssessmentLocation] [nvarchar](255) NULL,
        [TransportedByLE] [int] NULL,
        [TelevideoAssessment] [int] NULL,
        [CurrentDetoxSymptoms] [int] NULL,
        [HistoryOfDetoxSymptoms] [int] NULL,
        [PrimaryDSMDiagnosis] [nvarchar](255) NULL,
        [SecondaryDSMDiagnosis] [nvarchar](255) NULL,
        [CompletedByLastName] [nvarchar](255) NULL,
        [CompletedByFirstName] [nvarchar](255) NULL,
        [DateDispositionCompleted] [date] NULL,
        [TimeDispositionCompleted] [time](7) NULL,
        [RecommendedTransportMode] [int] NULL,
        [DateTransportedToFacility] [date] NULL,
        [TimeTransportedToFacility] [time](7) NULL,
        [FollowupContacted] [nvarchar](255) NULL,
        [FollowupReportedServiceHelpful] [nvarchar](255) NULL,
        [ContactAttempts] [nvarchar](255) NULL,
        [VoluntaryAdmissionRecommended] [nvarchar](255) NULL,
        [AdmissionAssessmentViaTelehealth] [nvarchar](255) NULL,
        [IsAdmitted] [nvarchar](255) NULL,
        [FirstHospitalization] [nvarchar](255) NULL,
        [PrimaryProblem] [nvarchar](255) NULL,
        [IntellectualDisability] [int] NULL,
        [MedicalInstability] [int] NULL,
        [MedicationIssues] [int] NULL,
        [PastTrauma] [int] NULL,
        [SubstanceAbuse] [int] NULL,
        [Drug] [int] NULL,
        [DrugRoute] [int] NULL,
        [DrugFrequency] [int] NULL,
        [HospAlternative] [nvarchar](255) NULL,
        [HospAltDisposition] [nvarchar](255) NULL,
        [Hospitalization] [nvarchar](255) NULL,
        [HospitalizationDisposition] [nvarchar](255) NULL,
        [SCS_Stf_Recommend] [nvarchar](255) NULL
    )
    insert INTO @t
    ([people_id],[actual_date],[NPI],[FileCreationDate],[FileCreationTime],[ProviderPatientNo],[LastName],[FirstName],[SSN],[DOB],[Gender],[Race],[Ethnicity],[ProviderPhoneAssessmentId],[CallEndDate],[CallEndTime],[DispatchDate],[DispatchTime],[CallDisposition],[DispositionOther],[Notes],[ProviderF2FAssessmentId],[AssessmentDate],[ArrivalTime],[ResidentialStatus],[County],[EmploymentStatus],[MaritalStatus],[MilitaryStatus],[NumArrests30Days],[AttendedSchoolLast3Months],[EducationLevel],[PrimaryPayorSource],[SecondaryPayorSource],[AnnualHouseholdIncome],[NumberInHousehold],[CurrentServices],[MHTreatmentDeclaration],[MOTStatus],[DurablePOA],[AssessmentLocation],[TransportedByLE],[TelevideoAssessment],[CurrentDetoxSymptoms],[HistoryOfDetoxSymptoms],[PrimaryDSMDiagnosis],[SecondaryDSMDiagnosis],[CompletedByLastName],[CompletedByFirstName],[DateDispositionCompleted],[TimeDispositionCompleted],[RecommendedTransportMode],[DateTransportedToFacility],[TimeTransportedToFacility],[FollowupContacted],[FollowupReportedServiceHelpful],[ContactAttempts],[VoluntaryAdmissionRecommended],[AdmissionAssessmentViaTelehealth],[IsAdmitted],[FirstHospitalization],[PrimaryProblem],[IntellectualDisability],[MedicalInstability],[MedicationIssues],[PastTrauma],[SubstanceAbuse],[Drug],[DrugRoute],[DrugFrequency],[HospAlternative],[HospAltDisposition],[Hospitalization],[HospitalizationDisposition],[SCS_Stf_Recommend])
    VALUES
    ('90F07844-746A-4347-82CA-39D4332B43F3','2013-09-25','1306875695','2014-02-12','15:19:37.0000000','108677','David','Joe','414555555','1999-01-23','2','1','2','59DC25C9-B659-42A3-B43D-26C741F9B929','2013-09-26','15:17:00.0000000',NULL,NULL,'1',NULL,NULL,NULL,NULL,NULL,NULL,'87',NULL,'6','4',NULL,NULL,NULL,'9','9',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'LastName','Alisha','2013-09-26','15:17:00.0000000',NULL,NULL,NULL,'0',NULL,NULL,NULL,NULL,'0',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL),
    ('90F07844-746A-4347-82CA-39D4332B43F3','2013-09-25','1306875695','2014-02-12','15:19:37.0000000','108677','David','Joe','414555555','1999-01-23','2','1','2',NULL,'2013-09-25','18:45:00.0000000','2013-09-25','18:51:00.0000000','4',NULL,NULL,'35159D47-32B2-445C-A905-019E191FDDE2','2013-09-25','19:22:00.0000000','13','47','12','6','4',NULL,'3','23','8','9','0','4','8','3','3','3','4','0','0','0','0','V71.09   ','V71.09','Tweed','A','2013-09-25','21:10:51.0000000','3',NULL,NULL,'1','1',NULL,'0','0','0',NULL,'2','3','3','3','3','2',NULL,NULL,NULL,'8','4',NULL,NULL,NULL)

    IF OBJECT_ID('tempdb.dbo.#Patient') IS NOT NULL drop table #Patient
    IF OBJECT_ID('tempdb.dbo.#Drugs') IS NOT NULL drop table #Drugs
    IF OBJECT_ID('tempdb.dbo.#Assessments') IS NOT NULL drop table #Assessments
    IF OBJECT_ID('tempdb.dbo.#HospAlt') IS NOT NULL drop table #HospAlt
    IF OBJECT_ID('tempdb.dbo.#HospDisp') IS NOT NULL drop table #HospDisp
    IF OBJECT_ID('tempdb.dbo.#PatientDistinct') IS NOT NULL drop table #PatientDistinct

    --Patient Distinct
    select distinct
    ProviderPatientNo
    into #PatientDistinct
    FROM @t


    --Patients
    select distinct
    NPI,
    FileCreationDate,
    FileCreationTime,
    ProviderPatientNo,
    ProviderF2FAssessmentId,
    ProviderPhoneAssessmentId,
    people_id, LastName,FirstName,
    SSN,[DOB],[Gender],[Race],[Ethnicity]
    into #Patient
    FROM @t

    --Assessments
    SELECT
    CallEndDate,
    CallEndTime,
    DispatchDate,
    DispatchTime,
    CallDisposition,
    DispositionOther,
    Notes,
    people_id,
    ProviderPatientNo,
    ProviderF2FAssessmentId,
    ProviderPhoneAssessmentId,
    AssessmentDate, 
    case when ArrivalTime is null then '07:00:00' else ArrivalTime end AS [ArrivalTime] ,
    ResidentialStatus AS [ResidentialStatus],
    County AS [County], 
    EmploymentStatus AS [EmploymentStatus], 
    MaritalStatus AS [MaritalStatus], 
    MilitaryStatus AS [MilitaryStatus],
    NumArrests30Days AS [NumArrests30Days], 
    AttendedSchoolLast3Months AS [AttendedSchoolLast3Months], 
    EducationLevel AS [EducationLevel], 
    PrimaryPayorSource AS [PrimaryPayorSource], 
    SecondaryPayorSource AS [SecondaryPayorSource], 
    AnnualHouseholdIncome AS [AnnualHouseholdIncome],
    NumberInHousehold AS [NumberInHousehold], 
    CurrentServices AS [CurrentServices], 
    MHTreatmentDeclaration AS [MHTreatmentDeclaration], 
    MOTStatus AS [MOTStatus], 
    DurablePOA AS [DurablePOA], 
    AssessmentLocation AS [AssessmentLocation], 
    TransportedByLE AS [TransportedByLE], 
    TelevideoAssessment AS [TelevideoAssessment],
    CurrentDetoxSymptoms AS [CurrentDetoxSymptoms], 
    HistoryOfDetoxSymptoms AS [HistoryOfDetoxSymptoms],
    PrimaryDSMDiagnosis AS [PrimaryDSMDiagnosis], 
    SecondaryDSMDiagnosis AS [SecondaryDSMDiagnosis],
    CompletedByLastName AS [CompletedByLastName], 
    CompletedByFirstName AS [CompletedByFirstName],
    DateDispositionCompleted AS [DateDispositionCompleted], 
    TimeDispositionCompleted AS [TimeDispositionCompleted], 
    RecommendedTransportMode AS [RecommendedTransportMode], 
    DateTransportedToFacility AS [DateTransportedToFacility], 
    TimeTransportedToFacility AS [TimeTransportedToFacility], 
    FollowupContacted AS [FollowupContacted],
    FollowupReportedServiceHelpful AS [FollowupReportedServiceHelpful], 
    ContactAttempts AS [ContactAttempts], 
    VoluntaryAdmissionRecommended AS [VoluntaryAdmissionRecommended], 
    AdmissionAssessmentViaTelehealth AS [AdmissionAssessmentViaTelehealth], 
    IsAdmitted AS [IsAdmitted], 
    FirstHospitalization AS [FirstHospitalization],
    PrimaryProblem AS [PrimaryProblem], 
    IntellectualDisability AS [IntellectualDisability], 
    MedicalInstability AS [MedicalInstability], 
    MedicationIssues AS [MedicationIssues], 
    PastTrauma AS [PastTrauma], 
    SubstanceAbuse AS [SubstanceAbuse]
    into #Assessments
    FROM @t

    --Drugs
    select ProviderF2FAssessmentId,
    Drug,
    DrugRoute,
    DrugFrequency
    into #Drugs
    from @t
    where ProviderF2FAssessmentId is not null

    --HospAlternative
    select 
    ProviderF2FAssessmentId,
    HospAlternative,
    HospAltDisposition
    into #HospAlt
    from @t
    where ProviderF2FAssessmentId is not null

    --Hospitalization
    select
    ProviderF2FAssessmentId,
    1 as Hospitalization,
    10 as HospitalizationDisposition
    into #HospDisp
    from @t
    where ProviderF2FAssessmentId is not null



    /*Create XML*/
    declare @output XML

    set @output =
    --Provider Data
    (
    SELECT  
    NPI as [NPI], 
    FileCreationDate as [FileCreationDate], 
    cast(FileCreationTime as time) FileCreationTime,
    (
    --Patient Data

    Select  
    Patient.ProviderPatientNo ,
    LastName as [LastName],
    FirstName as [FirstName],
    SSN as [SSN],
    DOB as [DOB],
    Gender as [Gender],
    Race as [Race],
    Ethnicity as [Ethnicity],      
           --Phone Assessment Data
           /*
           <ProviderPhoneAssessmentId>52854541</ProviderPhoneAssessmentId>
           <CallEndDate>2006-05-04</CallEndDate>
           <CallEndTime>01:01:01.001</CallEndTime>
           <DispatchDate>2006-05-04</DispatchDate>
           <DispatchTime>01:01:01.001</DispatchTime>
           <CallDisposition>1</CallDisposition>
           <DispositionOther>DispositionOther0</DispositionOther>
           <Notes>Notes0</Notes>
           */
           (
           Select  
           ProviderPhoneAssessmentId,
           CallEndDate,
           CallEndTime,
           DispatchDate,
           DispatchTime,
           CallDisposition,
           DispositionOther,
           Notes
           FROM #Assessments
           WHERE ProviderPhoneAssessmentId is NOT NULL and ProviderPhoneAssessmentId = Patient.ProviderPhoneAssessmentId
           FOR XML PATH(''), ELEMENTS, type) AS [PhoneAssessment/*],
              --F2FAssessment
              /*
              <ProviderF2FAssessmentId>4343</ProviderF2FAssessmentId>
              <AssessmentDate>2006-05-04</AssessmentDate>
              <ArrivalTime>01:01:01.001</ArrivalTime>
              <ResidentialStatus>1</ResidentialStatus>
              <County>1</County>
              <EmploymentStatus>1</EmploymentStatus>
              <MaritalStatus>1</MaritalStatus>
              <MilitaryStatus>1</MilitaryStatus>
              <NumArrests30Days>50</NumArrests30Days>
              <AttendedSchoolLast3Months>1</AttendedSchoolLast3Months>
              <EducationLevel>1</EducationLevel>
              <PrimaryPayorSource>1</PrimaryPayorSource>
              <SecondaryPayorSource>1</SecondaryPayorSource>
              <AnnualHouseholdIncome>0</AnnualHouseholdIncome>
              <NumberInHousehold>128</NumberInHousehold>
              <CurrentServices>1</CurrentServices>
              <MHTreatmentDeclaration>1</MHTreatmentDeclaration>
              <MOTStatus>1</MOTStatus>
              <DurablePOA>1</DurablePOA>
              <AssessmentLocation>1</AssessmentLocation>
              <TransportedByLE>false</TransportedByLE>
              <TelevideoAssessment>false</TelevideoAssessment>
              <CurrentDetoxSymptoms>false</CurrentDetoxSymptoms>
              <HistoryOfDetoxSymptoms>false</HistoryOfDetoxSymptoms>
              <PrimaryDSMDiagnosis>PrimaryDS</PrimaryDSMDiagnosis>
              <SecondaryDSMDiagnosis>Secondary</SecondaryDSMDiagnosis>
              <CompletedByLastName>CompletedByLastName2</CompletedByLastName>
              <CompletedByFirstName>CompletedByFirstName2</CompletedByFirstName>
              <DateDispositionCompleted>2006-05-04</DateDispositionCompleted>
              <TimeDispositionCompleted>01:01:01.001</TimeDispositionCompleted>
              <RecommendedTransportMode>1</RecommendedTransportMode>
              <DateTransportedToFacility>2006-05-04</DateTransportedToFacility>
              <TimeTransportedToFacility>01:01:01.001</TimeTransportedToFacility>
              <FollowupContacted>false</FollowupContacted>
              <FollowupReportedServiceHelpful>false</FollowupReportedServiceHelpful>
              <ContactAttempts>128</ContactAttempts>
              <VoluntaryAdmissionRecommended>false</VoluntaryAdmissionRecommended>
              <AdmissionAssessmentViaTelehealth>false</AdmissionAssessmentViaTelehealth>
              <IsAdmitted>false</IsAdmitted><FirstHospitalization>1</FirstHospitalization>
              <PrimaryProblem>1</PrimaryProblem><IntellectualDisability>1</IntellectualDisability>
              <MedicalInstability>1</MedicalInstability>
              <MedicationIssues>1</MedicationIssues>
              <PastTrauma>1</PastTrauma>
              <SubstanceAbuse>1</SubstanceAbuse>
              */
              (SELECT 
              ProviderF2FAssessmentId as [F2FAssessment/ProviderF2FAssessmentId],
              AssessmentDate as [F2FAssessment/AssessmentDate],
              [ArrivalTime]  as [F2FAssessment/ArrivalTime],
              ResidentialStatus  as [F2FAssessment/ResidentialStatus],
              County  as [F2FAssessment/County], 
              EmploymentStatus AS [F2FAssessment/EmploymentStatus], 
              MaritalStatus AS [F2FAssessment/MaritalStatus], 
              MilitaryStatus AS [F2FAssessment/MilitaryStatus],
              NumArrests30Days AS [F2FAssessment/NumArrests30Days], 
              AttendedSchoolLast3Months AS [F2FAssessment/AttendedSchoolLast3Months],
              EducationLevel AS [F2FAssessment/EducationLevel], 
              PrimaryPayorSource AS [F2FAssessment/PrimaryPayorSource], 
              SecondaryPayorSource AS [F2FAssessment/SecondaryPayorSource], 
              AnnualHouseholdIncome AS [F2FAssessment/AnnualHouseholdIncome],
              NumberInHousehold AS [F2FAssessment/NumberInHousehold], 
              CurrentServices AS [F2FAssessment/CurrentServices], 
              MHTreatmentDeclaration AS [F2FAssessment/MHTreatmentDeclaration], 
              MOTStatus AS [F2FAssessment/MOTStatus], 
              DurablePOA AS [F2FAssessment/DurablePOA], 
              AssessmentLocation AS [F2FAssessment/AssessmentLocation], 
              TransportedByLE AS [F2FAssessment/TransportedByLE], 
              TelevideoAssessment AS [F2FAssessment/TelevideoAssessment],
              CurrentDetoxSymptoms AS [F2FAssessment/CurrentDetoxSymptoms], 
              HistoryOfDetoxSymptoms AS [F2FAssessment/HistoryOfDetoxSymptoms],
              PrimaryDSMDiagnosis AS [F2FAssessment/PrimaryDSMDiagnosis], 
              SecondaryDSMDiagnosis AS [F2FAssessment/SecondaryDSMDiagnosis],
              CompletedByLastName AS [F2FAssessment/CompletedByLastName], 
              CompletedByFirstName AS [F2FAssessment/CompletedByFirstName],
              DateDispositionCompleted AS [F2FAssessment/DateDispositionCompleted], 
              TimeDispositionCompleted AS [F2FAssessment/TimeDispositionCompleted], 
              RecommendedTransportMode AS [F2FAssessment/RecommendedTransportMode], 
              ISNULL(CAST(DateTransportedToFacility as varchar(30)),'xsi:nil="true"') AS [F2FAssessment/DateTransportedToFacility], 
              ISNULL(CAST(TimeTransportedToFacility as varchar(30)),'xsi:nil="true"')AS [F2FAssessment/TimeTransportedToFacility], 
              FollowupContacted AS [F2FAssessment/FollowupContacted],
              FollowupReportedServiceHelpful AS [F2FAssessment/FollowupReportedServiceHelpful], 
              ContactAttempts AS [F2FAssessment/ContactAttempts], 
              VoluntaryAdmissionRecommended AS [F2FAssessment/VoluntaryAdmissionRecommended], 
              AdmissionAssessmentViaTelehealth AS [F2FAssessment/AdmissionAssessmentViaTelehealth], 
              IsAdmitted AS [F2FAssessment/IsAdmitted], 
              FirstHospitalization AS [F2FAssessment/FirstHospitalization],
              PrimaryProblem AS [F2FAssessment/PrimaryProblem], 
              IntellectualDisability AS [F2FAssessment/IntellectualDisability], 
              MedicalInstability AS [F2FAssessment/MedicalInstability], 
              MedicationIssues AS [F2FAssessment/MedicationIssues], 
              PastTrauma AS [F2FAssessment/PastTrauma], 
              SubstanceAbuse AS [F2FAssessment/SubstanceAbuse]
              ,
                 ( 
                        SELECT 
                        ISNULL(Drug,'') as Drug,
                        DrugRoute,
                        DrugFrequency
                        From #Drugs drugs
                        Where drugs.Drug is NOT NULL and drugs.ProviderF2FAssessmentId = #Assessments.ProviderF2FAssessmentId
                        FOR XML PATH(''), type) AS [F2FAssessment/F2FDrug] 
                    ,
                    ( 
                        SELECT 
                        HospAlternative,
                        HospAltDisposition
                        From #HospAlt HospAlt
                        Where HospAlt.ProviderF2FAssessmentId = #Assessments.ProviderF2FAssessmentId
                        FOR XML PATH(''), type) AS [F2FAssessment/F2FHospAlternative] 
                    ,
                    ( 
                        SELECT 
                        Hospitalization,
                        HospitalizationDisposition
                        From #HospDisp HospDisp
                        Where HospDisp.ProviderF2FAssessmentId = #Assessments.ProviderF2FAssessmentId
                        FOR XML PATH(''), type) AS [F2FAssessment/F2FHospitalization] 
              FROM #Assessments               
              Where ProviderF2FAssessmentId IS NOT NULL and ProviderF2FAssessmentId = Patient.ProviderF2FAssessmentId
              FOR XML PATH(''), ELEMENTS, type) AS [*] 

    FROM #Patient Patient 

    FOR XML PATH('Patient'), type
    )
    from @t
    group by NPI,FileCreationDate, FileCreationTime
    for xml path('')
    )

    ; with xmlnamespaces ('http://www.tn.gov/mental/Schemas/CrisisAssessment' AS "xsd", 'http://www.w3.org/2001/XMLSchema-instance' as "xsi")
    select @output FOR XML PATH(''),TYPE, ROOT('Provider')

以下是我目前获得的XML输出示例:

    <Provider xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.tn.gov/mental/Schemas/CrisisAssessment">
      <NPI>1306875695</NPI>
      <FileCreationDate>2014-02-12</FileCreationDate>
      <FileCreationTime>15:19:37</FileCreationTime>
      <Patient>
        <ProviderPatientNo>108677</ProviderPatientNo>
        <LastName>David</LastName>
        <FirstName>Joe</FirstName>
        <SSN>414555555</SSN>
        <DOB>1999-01-23</DOB>
        <Gender>2</Gender>
        <Race>1</Race>
        <Ethnicity>2</Ethnicity>
        <PhoneAssessment>
          <ProviderPhoneAssessmentId>59DC25C9-B659-42A3-B43D-26C741F9B929</ProviderPhoneAssessmentId>
          <CallEndDate>2013-09-26</CallEndDate>
          <CallEndTime>15:17:00</CallEndTime>
          <CallDisposition>1</CallDisposition>
        </PhoneAssessment>
      </Patient>
      <Patient>
        <ProviderPatientNo>108677</ProviderPatientNo>
        <LastName>David</LastName>
        <FirstName>Joe</FirstName>
        <SSN>414555555</SSN>
        <DOB>1999-01-23</DOB>
        <Gender>2</Gender>
        <Race>1</Race>
        <Ethnicity>2</Ethnicity>
        <F2FAssessment>
          <ProviderF2FAssessmentId>35159D47-32B2-445C-A905-019E191FDDE2</ProviderF2FAssessmentId>
          <AssessmentDate>2013-09-25</AssessmentDate>
          <ArrivalTime>19:22:00</ArrivalTime>
          <ResidentialStatus>13</ResidentialStatus>
          <County>47</County>
          <EmploymentStatus>12</EmploymentStatus>
          <MaritalStatus>6</MaritalStatus>
          <MilitaryStatus>4</MilitaryStatus>
          <AttendedSchoolLast3Months>3</AttendedSchoolLast3Months>
          <EducationLevel>23</EducationLevel>
          <PrimaryPayorSource>8</PrimaryPayorSource>
          <SecondaryPayorSource>9</SecondaryPayorSource>
          <AnnualHouseholdIncome>0</AnnualHouseholdIncome>
          <NumberInHousehold>4</NumberInHousehold>
          <CurrentServices>8</CurrentServices>
          <MHTreatmentDeclaration>3</MHTreatmentDeclaration>
          <MOTStatus>3</MOTStatus>
          <DurablePOA>3</DurablePOA>
          <AssessmentLocation>4</AssessmentLocation>
          <TransportedByLE>0</TransportedByLE>
          <TelevideoAssessment>0</TelevideoAssessment>
          <CurrentDetoxSymptoms>0</CurrentDetoxSymptoms>
          <HistoryOfDetoxSymptoms>0</HistoryOfDetoxSymptoms>
          <PrimaryDSMDiagnosis>V71.09   </PrimaryDSMDiagnosis>
          <SecondaryDSMDiagnosis>V71.09</SecondaryDSMDiagnosis>
          <CompletedByLastName>Tweed</CompletedByLastName>
          <CompletedByFirstName>A</CompletedByFirstName>
          <DateDispositionCompleted>2013-09-25</DateDispositionCompleted>
          <TimeDispositionCompleted>21:10:51</TimeDispositionCompleted>
          <RecommendedTransportMode>3</RecommendedTransportMode>
          <DateTransportedToFacility>xsi:nil="true"</DateTransportedToFacility>
          <TimeTransportedToFacility>xsi:nil="true"</TimeTransportedToFacility>
          <FollowupContacted>1</FollowupContacted>
          <FollowupReportedServiceHelpful>1</FollowupReportedServiceHelpful>
          <VoluntaryAdmissionRecommended>0</VoluntaryAdmissionRecommended>
          <AdmissionAssessmentViaTelehealth>0</AdmissionAssessmentViaTelehealth>
          <IsAdmitted>0</IsAdmitted>
          <PrimaryProblem>2</PrimaryProblem>
          <IntellectualDisability>3</IntellectualDisability>
          <MedicalInstability>3</MedicalInstability>
          <MedicationIssues>3</MedicationIssues>
          <PastTrauma>3</PastTrauma>
          <SubstanceAbuse>2</SubstanceAbuse>
          <F2FHospAlternative>
            <HospAlternative>8</HospAlternative>
            <HospAltDisposition>4</HospAltDisposition>
          </F2FHospAlternative>
          <F2FHospitalization>
            <Hospitalization>1</Hospitalization>
            <HospitalizationDisposition>10</HospitalizationDisposition>
          </F2FHospitalization>
        </F2FAssessment>
      </Patient>
    </Provider>

以下是我需要它的示例:

<Provider xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.tn.gov/mental/Schemas/CrisisAssessment">
  <NPI>1306875695</NPI>
  <FileCreationDate>2014-02-12</FileCreationDate>
  <FileCreationTime>15:19:37</FileCreationTime>
  <Patient>
    <ProviderPatientNo>108677</ProviderPatientNo>
    <LastName>David</LastName>
    <FirstName>Joe</FirstName>
    <SSN>414555555</SSN>
    <DOB>1999-01-23</DOB>
    <Gender>2</Gender>
    <Race>1</Race>
    <Ethnicity>2</Ethnicity>
    <PhoneAssessment>
      <ProviderPhoneAssessmentId>59DC25C9-B659-42A3-B43D-26C741F9B929</ProviderPhoneAssessmentId>
      <CallEndDate>2013-09-26</CallEndDate>
      <CallEndTime>15:17:00</CallEndTime>
      <CallDisposition>1</CallDisposition>
    </PhoneAssessment>
    <F2FAssessment>
      <ProviderF2FAssessmentId>35159D47-32B2-445C-A905-019E191FDDE2</ProviderF2FAssessmentId>
      <AssessmentDate>2013-09-25</AssessmentDate>
      <ArrivalTime>19:22:00</ArrivalTime>
      <ResidentialStatus>13</ResidentialStatus>
      <County>47</County>
      <EmploymentStatus>12</EmploymentStatus>
      <MaritalStatus>6</MaritalStatus>
      <MilitaryStatus>4</MilitaryStatus>
      <AttendedSchoolLast3Months>3</AttendedSchoolLast3Months>
      <EducationLevel>23</EducationLevel>
      <PrimaryPayorSource>8</PrimaryPayorSource>
      <SecondaryPayorSource>9</SecondaryPayorSource>
      <AnnualHouseholdIncome>0</AnnualHouseholdIncome>
      <NumberInHousehold>4</NumberInHousehold>
      <CurrentServices>8</CurrentServices>
      <MHTreatmentDeclaration>3</MHTreatmentDeclaration>
      <MOTStatus>3</MOTStatus>
      <DurablePOA>3</DurablePOA>
      <AssessmentLocation>4</AssessmentLocation>
      <TransportedByLE>0</TransportedByLE>
      <TelevideoAssessment>0</TelevideoAssessment>
      <CurrentDetoxSymptoms>0</CurrentDetoxSymptoms>
      <HistoryOfDetoxSymptoms>0</HistoryOfDetoxSymptoms>
      <PrimaryDSMDiagnosis>V71.09   </PrimaryDSMDiagnosis>
      <SecondaryDSMDiagnosis>V71.09</SecondaryDSMDiagnosis>
      <CompletedByLastName>Tweed</CompletedByLastName>
      <CompletedByFirstName>A</CompletedByFirstName>
      <DateDispositionCompleted>2013-09-25</DateDispositionCompleted>
      <TimeDispositionCompleted>21:10:51</TimeDispositionCompleted>
      <RecommendedTransportMode>3</RecommendedTransportMode>
      <DateTransportedToFacility>xsi:nil="true"</DateTransportedToFacility>
      <TimeTransportedToFacility>xsi:nil="true"</TimeTransportedToFacility>
      <FollowupContacted>1</FollowupContacted>
      <FollowupReportedServiceHelpful>1</FollowupReportedServiceHelpful>
      <VoluntaryAdmissionRecommended>0</VoluntaryAdmissionRecommended>
      <AdmissionAssessmentViaTelehealth>0</AdmissionAssessmentViaTelehealth>
      <IsAdmitted>0</IsAdmitted>
      <PrimaryProblem>2</PrimaryProblem>
      <IntellectualDisability>3</IntellectualDisability>
      <MedicalInstability>3</MedicalInstability>
      <MedicationIssues>3</MedicationIssues>
      <PastTrauma>3</PastTrauma>
      <SubstanceAbuse>2</SubstanceAbuse>
      <F2FHospAlternative>
        <HospAlternative>8</HospAlternative>
        <HospAltDisposition>4</HospAltDisposition>
      </F2FHospAlternative>
      <F2FHospitalization>
        <Hospitalization>1</Hospitalization>
        <HospitalizationDisposition>10</HospitalizationDisposition>
      </F2FHospitalization>
    </F2FAssessment>
  </Patient>
</Provider>

非常感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:0)

这正如我将在我的测试代码中显示的那样。但是,我们遇到的一个问题是,如果有人同时拥有F2FAssessment和PhoneAssessment,它将生成多个标签。

这是因为您插入人员记录2次。 第一次插入:人员数据+ phoneAssesment数据 第二次插入:Peson数据+ F2FAssessment

表t有2条NPI记录:1306875695

我的建议是修改@t表,

  1. 创建@ temp1表,@ temp1应该只有人员数据+ phoneAssesment
  2. 创建@ temp2表,@ temp2只包含PersonID + F2FAssessment(PErsonId充当外键)
  3. 将数据插入两个表格。
  4. 内部将表@ temp1和@ temp2连接为表@t。使用PersonId作为连接条件。
  5. 现在表@t只有1条NPI记录:1306875695
  6. 尝试这个建议,希望这是有帮助的。

答案 1 :(得分:0)

如果您只想要一个Patient元素,请创建PhoneAssessmentF2F*个语句子查询:

WITH XMLNAMESPACES (DEFAULT 'http://www.tn.gov/mental/Schemas/CrisisAssessment')
SELECT 
    [NPI],
    [FileCreationDate],
    [FileCreationTime],
    (
        SELECT 
            ProviderPatientNo,
            LastName, FirstName,
            SSN, DOB, Gender,
            Race, Ethnicity,
            (
                SELECT 
                    ProviderPhoneAssessmentId, CallEndDate, CallEndTime, CallDisposition
                FROM @t pa
                WHERE ProviderPhoneAssessmentId is not null
                    and pa.ProviderPatientNo = p.ProviderPatientNo
                FOR XML PATH('PhoneAssesment'), TYPE, ELEMENTS XSINIL
            ),
            (
                SELECT 
                    ProviderF2FAssessmentId, 
                    AssessmentDate, ArrivalTime, ResidentialStatus, County, EmploymentStatus, 
                    MaritalStatus, MilitaryStatus, AttendedSchoolLast3Months, EducationLevel, 
                    PrimaryPayorSource, SecondaryPayorSource, AnnualHouseholdIncome, 
                    NumberInHousehold, CurrentServices, MHTreatmentDeclaration, MOTStatus, 
                    DurablePOA, AssessmentLocation, TransportedByLE, TelevideoAssessment, 
                    CurrentDetoxSymptoms, HistoryOfDetoxSymptoms, PrimaryDSMDiagnosis, 
                    SecondaryDSMDiagnosis, CompletedByLastName, CompletedByFirstName, 
                    DateDispositionCompleted, TimeDispositionCompleted, RecommendedTransportMode, 
                    DateTransportedToFacility, TimeTransportedToFacility, FollowupContacted, 
                    FollowupReportedServiceHelpful, VoluntaryAdmissionRecommended, 
                    AdmissionAssessmentViaTelehealth, IsAdmitted, PrimaryProblem, 
                    IntellectualDisability, MedicalInstability, MedicationIssues, PastTrauma, 
                    SubstanceAbuse,
                    HospAlternative as [F2FHospAlternative/HospAlternative],
                    HospAltDisposition as [F2FHospAlternative/HospAltDisposition],
                    Hospitalization as [F2FHospitalization/Hospitalization],
                    HospitalizationDisposition as [F2FHospitalization/HospitalizationDisposition]
                FROM @t f2f
                WHERE f2f.ProviderF2FAssessmentId is not null 
                    and f2f.ProviderPatientNo = p.ProviderPatientNo
                GROUP BY ProviderF2FAssessmentId, 
                    AssessmentDate, ArrivalTime, ResidentialStatus, County, EmploymentStatus, 
                    MaritalStatus, MilitaryStatus, AttendedSchoolLast3Months, EducationLevel, 
                    PrimaryPayorSource, SecondaryPayorSource, AnnualHouseholdIncome, 
                    NumberInHousehold, CurrentServices, MHTreatmentDeclaration, MOTStatus, 
                    DurablePOA, AssessmentLocation, TransportedByLE, TelevideoAssessment, 
                    CurrentDetoxSymptoms, HistoryOfDetoxSymptoms, PrimaryDSMDiagnosis, 
                    SecondaryDSMDiagnosis, CompletedByLastName, CompletedByFirstName, 
                    DateDispositionCompleted, TimeDispositionCompleted, RecommendedTransportMode, 
                    DateTransportedToFacility, TimeTransportedToFacility, FollowupContacted, 
                    FollowupReportedServiceHelpful, VoluntaryAdmissionRecommended, 
                    AdmissionAssessmentViaTelehealth, IsAdmitted, PrimaryProblem, 
                    IntellectualDisability, MedicalInstability, MedicationIssues, PastTrauma, 
                    SubstanceAbuse, HospAlternative, HospAltDisposition, Hospitalization,
                    HospitalizationDisposition
                FOR XML PATH('F2FAssessment'), TYPE, ELEMENTS XSINIL
            )
        FROM @t p
        GROUP BY ProviderPatientNo, LastName, FirstName, SSN, DOB, Gender, Race, Ethnicity
        FOR XML PATH('Patient'), TYPE, ELEMENTS XSINIL
    )
FROM (SELECT TOP(1) [NPI], [FileCreationDate], [FileCreationTime] FROM @t) as FileHeader
FOR XML PATH('Provider'), ELEMENTS XSINIL

另外,我猜你希望xsi:nil="true"成为一个属性。这是通过XSINIL option实现的。此外,您引用命名空间,但不要使用它。您的意思是将其设为默认值(WITH XMLNAMESPACES (DEFAULT 'http://www.tn.gov/mental/Schemas/CrisisAssessment'))吗?

产地:

<Provider xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.tn.gov/mental/Schemas/CrisisAssessment">
  <NPI>1306875695</NPI>
  <FileCreationDate>2014-02-12</FileCreationDate>
  <FileCreationTime>15:19:37</FileCreationTime>
  <Patient xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.tn.gov/mental/Schemas/CrisisAssessment">
    <ProviderPatientNo>108677</ProviderPatientNo>
    <LastName>David</LastName>
    <FirstName>Joe</FirstName>
    <SSN>414555555</SSN>
    <DOB>1999-01-23</DOB>
    <Gender>2</Gender>
    <Race>1</Race>
    <Ethnicity>2</Ethnicity>
    <PhoneAssesment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.tn.gov/mental/Schemas/CrisisAssessment">
      <ProviderPhoneAssessmentId>59DC25C9-B659-42A3-B43D-26C741F9B929</ProviderPhoneAssessmentId>
      <CallEndDate>2013-09-26</CallEndDate>
      <CallEndTime>15:17:00</CallEndTime>
      <CallDisposition>1</CallDisposition>
    </PhoneAssesment>
    <F2FAssessment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.tn.gov/mental/Schemas/CrisisAssessment">
      <ProviderF2FAssessmentId>35159D47-32B2-445C-A905-019E191FDDE2</ProviderF2FAssessmentId>
      <AssessmentDate>2013-09-25</AssessmentDate>
      <ArrivalTime>19:22:00</ArrivalTime>
      <ResidentialStatus>13</ResidentialStatus>
      <County>47</County>
      <EmploymentStatus>12</EmploymentStatus>
      <MaritalStatus>6</MaritalStatus>
      <MilitaryStatus>4</MilitaryStatus>
      <AttendedSchoolLast3Months>3</AttendedSchoolLast3Months>
      <EducationLevel>23</EducationLevel>
      <PrimaryPayorSource>8</PrimaryPayorSource>
      <SecondaryPayorSource>9</SecondaryPayorSource>
      <AnnualHouseholdIncome>0</AnnualHouseholdIncome>
      <NumberInHousehold>4</NumberInHousehold>
      <CurrentServices>8</CurrentServices>
      <MHTreatmentDeclaration>3</MHTreatmentDeclaration>
      <MOTStatus>3</MOTStatus>
      <DurablePOA>3</DurablePOA>
      <AssessmentLocation>4</AssessmentLocation>
      <TransportedByLE>0</TransportedByLE>
      <TelevideoAssessment>0</TelevideoAssessment>
      <CurrentDetoxSymptoms>0</CurrentDetoxSymptoms>
      <HistoryOfDetoxSymptoms>0</HistoryOfDetoxSymptoms>
      <PrimaryDSMDiagnosis>V71.09   </PrimaryDSMDiagnosis>
      <SecondaryDSMDiagnosis>V71.09</SecondaryDSMDiagnosis>
      <CompletedByLastName>Tweed</CompletedByLastName>
      <CompletedByFirstName>A</CompletedByFirstName>
      <DateDispositionCompleted>2013-09-25</DateDispositionCompleted>
      <TimeDispositionCompleted>21:10:51</TimeDispositionCompleted>
      <RecommendedTransportMode>3</RecommendedTransportMode>
      <DateTransportedToFacility xsi:nil="true" />
      <TimeTransportedToFacility xsi:nil="true" />
      <FollowupContacted>1</FollowupContacted>
      <FollowupReportedServiceHelpful>1</FollowupReportedServiceHelpful>
      <VoluntaryAdmissionRecommended>0</VoluntaryAdmissionRecommended>
      <AdmissionAssessmentViaTelehealth>0</AdmissionAssessmentViaTelehealth>
      <IsAdmitted>0</IsAdmitted>
      <PrimaryProblem>2</PrimaryProblem>
      <IntellectualDisability>3</IntellectualDisability>
      <MedicalInstability>3</MedicalInstability>
      <MedicationIssues>3</MedicationIssues>
      <PastTrauma>3</PastTrauma>
      <SubstanceAbuse>2</SubstanceAbuse>
      <F2FHospAlternative>
        <HospAlternative>8</HospAlternative>
        <HospAltDisposition>4</HospAltDisposition>
      </F2FHospAlternative>
      <F2FHospitalization>
        <Hospitalization xsi:nil="true" />
        <HospitalizationDisposition xsi:nil="true" />
      </F2FHospitalization>
    </F2FAssessment>
  </Patient>
</Provider>