2017-03-21 45 views
0

我有3個臨時表全部由3個獨立的查詢填充,並且以1對1關係相互關聯,這些表是DemographicRecord,GPRegistrationDetails,MaternityBookingDetails。所有3個列都不相同,但每個列都共享PatientID密鑰。我的問題是使用XML路徑,我如何根據下面的格式從3個相關的數據集中輸出XML。使用TSQL和XML路徑來生成XML輸出

<MAT001MothersDemographics> 
     <LocalPatientIdMother>BLANKED</LocalPatientIdMother> 
     <OrgCodeLocalPatientIdMother>BLANKED</OrgCodeLocalPatientIdMother> 
     <OrgCodeRes>BLANKED</OrgCodeRes> 
     <NHSNumberMother>BLANKED</NHSNumberMother> 
     <NHSNumberStatusMother>BLANKED</NHSNumberStatusMother> 
     <PersonBirthDateMother>BLANKED</PersonBirthDateMother> 
     <Postcode>BLANKED</Postcode> 
     <EthnicCategoryMother>BLANKED</EthnicCategoryMother> 
     <PersonDeathDateTimeMother>BLANKED</PersonDeathDateTimeMother> 
     <MAT003GPPracticeRegistration> 
      <LocalPatientIdMother>BLANKED</LocalPatientIdMother> 
      <OrgCodeGMPMother>BLANKED</OrgCodeGMPMother> 
      <StartDateGMPRegistration>BLANKED</StartDateGMPRegistration> 
      <EndDateGMPRegistration>BLANKED</EndDateGMPRegistration> 
      <OrgCodeCommissioner>BLANKED</OrgCodeCommissioner> 
     </MAT003GPPracticeRegistration> 
     <MAT101BookingAppointmentDetails> 
      <AntenatalAppDate>BLANKED</AntenatalAppDate> 
      <LocalPatientIdMother>BLANKED</LocalPatientIdMother> 
      <EDDAgreed>BLANKED</EDDAgreed> 
      <EDDMethodAgreed>BLANKED</EDDMethodAgreed> 
      <PregnancyFirstContactDate>BLANKED</PregnancyFirstContactDate> 
      <PregnancyFirstContactCareProfessionalType>BLANKED</PregnancyFirstContactCareProfessionalType> 
      <LastMenstrualPeriodDate>BLANKED</LastMenstrualPeriodDate> 
      <PhysicalDisabilityStatusIndMother>BLANKED</PhysicalDisabilityStatusIndMother> 
      <FirstLanguageEnglishIndMother>BLANKED</FirstLanguageEnglishIndMother> 
      <EmploymentStatusMother>BLANKED</EmploymentStatusMother> 
      <SupportStatusMother>BLANKED</SupportStatusMother> 
      <EmploymentStatusPartner>BLANKED</EmploymentStatusPartner> 
      <PreviousCaesareanSections>BLANKED</PreviousCaesareanSections> 
      <PreviousLiveBirths>BLANKED</PreviousLiveBirths> 
      <PreviousStillBirths>BLANKED</PreviousStillBirths> 
      <PreviousLossesLessThan24Weeks>BLANKED</PreviousLossesLessThan24Weeks> 
      <SubstanceUseStatus>BLANKED</SubstanceUseStatus> 
      <SmokingStatus>BLANKED</SmokingStatus> 
      <CigarettesPerDay>BLANKED</CigarettesPerDay> 
      <AlcoholUnitsPerWeek>BLANKED</AlcoholUnitsPerWeek> 
      <FolicAcidSupplement>BLANKED</FolicAcidSupplement> 
      <MHPredictionDetectionIndMother>BLANKED</MHPredictionDetectionIndMother> 
      <PersonWeight>BLANKED</PersonWeight> 
      <PersonHeight>BLANKED</PersonHeight> 
      <ComplexSocialFactorsInd>BLANKED</ComplexSocialFactorsInd> 
     </MAT101BookingAppointmentDetails> 
</MAT001MothersDemographics> 

到目前爲止,我曾嘗試:

SELECT 
(SELECT * FROM #temp2 
JOIN #temp ON #temp2.LocalPatientIdMother = #temp.LocalPatientIdMother 
JOIN #temp3 ON #temp2.LocalPatientIdMother = #temp3.LocalPatientIdMother 
FOR XML PATH('MAT001'), TYPE) AS 'MAT001MothersDemographics' 
FOR XML PATH(''), ROOT('root') 

但這不是正確的形狀,可有人提醒我如何使用TSQL和有效地XML PATH這樣我就可以產生上面的輸出?我目前正在獲取每個記錄在其他數據顯示之前重複的人口統計數據?

<MAT001MothersDemographics> 
    <MAT001> 
     <LocalPatientIdMother>BLANKED</LocalPatientIdMother> 
     <OrgCodeLocalPatientIdMother>BLANKED</OrgCodeLocalPatientIdMother> 
     <OrgCodeRes>BLANKED</OrgCodeRes> 
     <NHSNumberMother>BLANKED</NHSNumberMother> 
     <NHSNumberStatusMother>BLANKED</NHSNumberStatusMother> 
     <PersonBirthDateMother>BLANKED</PersonBirthDateMother> 
     <Postcode>BLANKED</Postcode> 
     <EthnicCategoryMother>BLANKED</EthnicCategoryMother> 
     <PersonDeathDateTimeMother>BLANKED</PersonDeathDateTimeMother> 
     </MAT001> 
    </MAT001MothersDemographics> 
    <MAT001MothersDemographics> 
    <MAT001> 
     <LocalPatientIdMother>BLANKED</LocalPatientIdMother> 
     <OrgCodeLocalPatientIdMother>BLANKED</OrgCodeLocalPatientIdMother> 
     <OrgCodeRes>BLANKED</OrgCodeRes> 
     <NHSNumberMother>BLANKED</NHSNumberMother> 
     <NHSNumberStatusMother>BLANKED</NHSNumberStatusMother> 
     <PersonBirthDateMother>BLANKED</PersonBirthDateMother> 
     <Postcode>BLANKED</Postcode> 
     <EthnicCategoryMother>BLANKED</EthnicCategoryMother> 
     <PersonDeathDateTimeMother>BLANKED</PersonDeathDateTimeMother> 
     </MAT001> 
    </MAT001MothersDemographics> 

非常感謝

+0

您是否可以爲臨時表提供一些示例(清理過的)數據? –

+0

嗨丹謝謝,查詢是巨大的,所以我不會粘貼在這裏,但是,本質上他們填充3表 – jimmy8ball

+0

如果它有幫助臨時是MAT003,temp2是MAT001和temp3是MAT101和表匹配架構在第一個XML塊 – jimmy8ball

回答

1

我必須承認,你的問題是很清楚...你張貼了很多不需要的細節(如大個XML),但你不喜歡提供必要的信息表的結構和樣本數據。未來,請閱讀How to ask a good SQL questionHow to create a MCVE

但是 - 我的魔晶球從清洗回來! - 我嘗試快速射擊:

SELECT t.* 
     ,(
     SELECT * 
     FROM #temp2 AS t2 
     WHERE t.LocalPatientIdMother=t2.LocalPatientIdMother 
     FOR XML PATH('MAT003GPPracticeRegistration'),TYPE 
     ) AS [*] 
     ,(
     SELECT * 
     FROM #temp3 AS t3 
     WHERE t.LocalPatientIdMother=t3.LocalPatientIdMother 
     FOR XML PATH('MAT101BookingAppointmentDetail'),TYPE 
     ) AS [*] 
FROM #temp AS t 
FOR XML PATH('MAT001MothersDemographics'); 

這將返回的#temp1所有列和意志巢#temp2#temp3相關行。這是基於這樣的假設,即每個表中只有一個給定ID的記錄...

+0

這正是我所需要的,我會注意到您對太多信息和沒有足夠上下文的意見,但這正是我需要,非常感謝。 – jimmy8ball