2014-02-20 494 views
0

我希望我能得到一些幫助,我一直在一個比較複雜的問題的最後一部分。SQL到XML輸出

我們必須從生產,我們正在生成SQL表中的XML文件。

爲核心的XML需要三個要素。 病人 電話評估 F2F評估

這是工作,因爲我會在我的測試代碼中顯示。但是,我們遇到的一個問題是,如果某人同時擁有F2F評估和電話評估,則會生成多個標籤。

如果大家可以給我一些關於解決這個問題的最佳方法,那麼只有一個Patient標籤包含所有可能的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> 

任何幫助,你可以提供將不勝感激。

回答

0

這是工作,我會在我的測試代碼顯示。但是,我們遇到的一個問題是,如果某人同時擁有F2F評估和電話評估,則會生成多個標籤。

這發生,因爲您將個人記錄的2倍。 第一插入:Person數據+ phoneAssesment數據 第二插件:Peson數據+ F2FAssessment

表T有2個記錄人與NPI:1306875695

我的建議是修改@t表,

  1. 創建@ temp1目錄表中,@ temp1中應該只有人數據+ phoneAssesment
  2. 創建@ TEMP2表中,@ TEMP2只包含是PersonID + F2FAssessment(PERSONID充當外鍵)
  3. 將數據插入到兩個表中。
  4. Inner將table @ temp1和@ temp2作爲表@t加入。使用PersonId作爲連接條件。
  5. 現在表@t將只有1記錄NPI:1306875695

試試這個建議,希望這是幫助。

+0

這工作。謝謝你爲我清理那個心理障礙。 – user1441145

+0

歡迎您,祝您有個愉快的一天先生 – cyan

+0

儘管這樣做的確可以解決評估問題,但它會產生一個新問題,因爲有時候我可以擁有多個PhoneAssessments,但每個需要在他們自己的單獨元素下面存在。目前,每次手機評估都應該在一次手機評估下進行,因爲它應該生成多個條目。這可能不會反映在我發佈的測試數據中。希望這是有道理的,任何建議,你可以提供將不勝感激。 – user1441145

0

如果你只想要一個病人的元素,使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> 
+0

這真的很接近,但它遺漏了F2FDrug元素。 – user1441145

+0

另外,我不確定如何正確添加提供者標記以使其成爲根。如果你能詳細說明它會非常有幫助。 – user1441145

+0

@ user1441145,我的目的是演示如何去做,而不是爲你做。我給出的例子包括子查詢,您可以添加儘可能多的數據來生成您正在查找的元素。 – Mitch