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
部分将为每个客户端显示一次。谢谢。
<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');
答案 0 :(得分:1)
如果没有样本数据,这是一次盲目飞行,但您可以尝试这样的事情:
SELECT
consent_type AS [@constent_type]
, state_code AS [@state_code]
, suburb AS [@suburb]
, sex_code AS [@sex_code]
, postcode AS [@postcode]
, CarerPrimary AS [service_user_response/@value]
,'CarerPrimary' AS [service_user_response/@question_fieldname]
, CarerResidence AS [service_user_response/@value]
,'CarerResidence' AS [service_user_response/@question_fieldname]
, CarerRelationship AS [service_user_response/@value]
,'CarerRelationship' AS [service_user_response/@question_fieldname]
, service_type_outlet_codeno AS [service_user_outlet/@service_type_outlet_codeno]
, last_service_date AS [service_user_outlet/@last_service_date]
, service_hours AS [service_user_outlet/service_hours/@service_hours]
, to_date AS [service_user_outlet/service_hours/@to_date]
, from_date AS [service_user_outlet/service_hours/@from_date]
FROM
TABLEX
FOR XML PATH('service_user'),ROOT('agency');
FOR XML PATH
允许将XPath
定义为列别名。属性标有@
符号。
示例中的service_hours
看起来好像是1:n
,但查询看起来并非如此。