2
SELECT
consent_type, state_code,
suburb, sex_code, postcode,
CarerPrimary, CarerResidence, CarerRelationship,
service_type_outlet_codeno,
last_service_date, service_hours,
to_date, from_date
FROM
TABLEX
この単一のテーブルから次のXML形式を取得するにはどうすればよいですか?最も紛らわしいのはCarerPrimary
,CarerResidence
、CarerRelationship
部分です。この表には複数のクライアントの情報が含まれているため、クライアントごとにservice_user
の部分が一度表示されます。ありがとう。複数のXML形式のSQL Server単一テーブル
<agency>
<service_user consent_type="1" state_code="2" suburb="Croydon" sex_code="1" postcode="3136">
<service_user_response value="1" question_fieldname="CarerPrimary"/>
<service_user_response value="1" question_fieldname="CarerResidence"/>
<service_user_response value="9" question_fieldname="CarerRelationship"/>
<service_user_outlet service_type_outlet_codeno="8376784" last_service_date="02/9/2016" >
<service_hours service_hours="240" to_date="15/8/2016" from_date="09/8/2016"/>
<service_hours service_hours="90" to_date="02/9/2016" from_date="02/9/2016"/>
</service_user_outlet>
</service_user>
<service_user .... >
</service_user>
</agency>
新しいコード、
SELECT PostalCode AS [@Postcode]
,IIF (HC.HasCarer = 'true', 'Y', 'N') as [@has_carer_type]
,convert(date, [BirthDate], 111) AS [@BirthDate]
,'N' as [@birth_date_est_ind]
,IIF (new_gender = 6, 3, new_gender) AS [@sex_code]
--SLK
,City AS [@Suburb]
,DEX.[dbo].[CRM2DEX]('State',[new_Address1State], 0) AS [@StateCode]
,'Y' as [@consent_type]
--,CONCAT ([FirstName], ' ' + [MiddleName]) AS GivenName
-- ,[LastName] AS FamilyName
-- ,cb.new_ClientID AS ClientId
/********service_user_response*******/
,HC.CarerPrimary AS [service_user_response/@value]
,'CarerPrimary' AS [service_user_response/@question_fieldname]
,HC.CarerResidence AS [service_user_response/@value]
,'CarerResidence' AS [service_user_response/@question_fieldname]
,dex.dbo.CRM2QDC('Relationship', default, HC.new_Relationship) AS [service_user_response/@value]
,'CarerRelationship' AS [service_user_response/@question_fieldname]
--,IIF (new_CarerAgeGroup IS NULL, 99, new_CarerAgeGroup) AS CarerAgeGroup
,IIF (new_indigenoustatus = 9, NULL, new_indigenoustatus) AS [service_user_response/@value]
,'IndigenousStatus' AS [service_user_response/@question_fieldname]
,COALESCE (Country.[new_Code], '0003') AS [service_user_response/@value]
,'BirthCountry' AS [service_user_response/@question_fieldname]
,COALESCE (Lang.[new_Code], '0002') AS [service_user_response/@value]
,'Language' AS [service_user_response/@question_fieldname]
,new_interpreter AS [service_user_response/@value]
,'InterpreterServices' AS [service_user_response/@question_fieldname]
,new_communicationmethod AS [service_user_response/@value]
,'CommunicationMethod' AS [service_user_response/@question_fieldname]
,IIF (cb.new_livingarrangements = 9, 99, cb.new_livingarrangements) AS [service_user_response/@value]
,'LivingArrangements' AS [service_user_response/@question_fieldname]
,cb.new_residentialsetting AS [service_user_response/@value]
,'ResidentialSetting' AS [service_user_response/@question_fieldname]
,new_employmentstatus AS [service_user_response/@value]
,'LabourForceStatus' AS [service_user_response/@question_fieldname]
,IIF (new_primaryincomesource = 7, 97, new_primaryincomesource) AS [service_user_response/@value]
,'IncomeSource' AS [service_user_response/@question_fieldname]
--,(SELECT IIF (Disability IS NULL, 'false', 'true') from HasDisablity where ClientID = CB.ContactId) AS HasDisabilities
--,IIF (new_client is NULL, NULL, (select coalesce (new_name, null) FROM Health AS D where D.new_contact = CB.ContactId for xml path ('DisabilityCode'), type)) AS Disabilities
--,(SELECT IIF (new_name IS NULL, NULL, (select H2.new_name from Health AS H2 where H2.new_contact = CB.ContactId
-- for xml path ('DisabilityCode'), type)) FROM Health AS D where D.new_contact = CB.ContactId) AS Disabilities
-- ,DEX.[dbo].[CRM2DEX]('AccommodationType',C.new_residentialsetting, 0) AS AccommodationTypeCode
-- ,DEX.[dbo].[CRM2DEX]('DVACardStatus',[new_DVAentitlement], 0) AS DVACardStatusCode
--,COALESCE (HasCarer, 'false') AS HasCarer
-- ,DEX.[dbo].[CRM2DEX]('HouseholdComposition', C.new_livingarrangements, 0) AS HouseholdCompositionCode
FROM [FOCUSCRM_MSCRM].[dbo].[ContactBase] AS CB
INNER JOIN
(SELECT [ParentId]
,[Line1]
,[Line2]
,[City]
,[PostalCode]
FROM [FOCUSCRM_MSCRM].[dbo].[CustomerAddressBase]
WHERE addressNumber = 1) AS A ON CB.ContactId = A.ParentId
LEFT OUTER JOIN
(SELECT [new_countryId]
,[new_name]
,[new_Code]
FROM [FOCUSCRM_MSCRM].[dbo].[new_countryBase]) AS Country ON CB.new_CountryofBirth = Country.new_countryId
LEFT OUTER JOIN
(SELECT [new_languageId]
,[new_name]
,[new_Code]
FROM [FOCUSCRM_MSCRM].[dbo].[new_languageBase]) AS Lang ON CB.new_PreferredLanguage = Lang.new_languageId
LEFT OUTER JOIN
(SELECT new_ClientName, new_livingarrangements, new_residentialsetting
FROM (
SELECT [new_ClientName]
,row_number() over (Partition by new_ClientName ORDER BY ModifiedOn DESC) AS R
,new_AssessmentDate
,new_livingarrangements
,new_residentialsetting
FROM [FOCUSCRM_MSCRM].[dbo].[new_assessmentBase] AS AB1
WHERE statecode = 0
) AS T1
WHERE R = 1) AS C ON CB.ContactId = C.new_ClientName
LEFT OUTER JOIN HasCarer AS HC ON HC.new_clientname = cb.contactID
WHERE CB.new_clientID IN (SELECT DISTINCT new_clientID from TS)
and statecode = 0
--) AS T GROUP BY ClientID having count(ClientId)>1
FOR XML PATH('service_user'),ROOT('agency');
ありがとう@Shnugo。あなたはservice_hoursについて正しいです。それは1:nです。私にエラーを与える私の新しいクエリを見てください。結合はいくつかのテーブルといくつかのCTEです。列名 'service_user_response/@ value'が繰り返されます。同じXMLタグで同じ属性を複数回生成することはできません。 – MohammedS
@MohammedSコードを必要最小限に減らそうとしています...疑問がある場合、ネストされた '1:n'関連のサブノードを作成するには、単に'(SELECT ... FROM ... WHERE ... FOR XML PATH( 'xyz')、TYPE)AS [*] 'を適切な場所に置きます。サブ選択は、適合行をフィルタリングするために 'WHERE'句を使用する必要があります。そして、カメレオンの質問を避けてください! – Shnugo
ありがとう@Shnugo。サブノードは問題ではありません。私の問題は、列からvalueとquestion_fieldnameを表現する方法でした。あなたのコードを試しましたが、 "列名 'service_user_response/@ value'が繰り返されていると不平を言います。同じ属性を同じXMLタグに複数回生成することはできません。この問題を解決するには? – MohammedS