目前我有一个输出到XML的大型SQL Server脚本。总结其中一个节点是重复的。以下是相关片段。我正在寻找将一些节点合并为一个但似乎无法使语法正确。请参阅下面的当前和所需的输出示例。
DECLARE @CRWGroups TABLE (ClientID INT, intRegistrationWizardID INT, nvhProfileItemName VARCHAR(MAX), intProfileItemID INT, bitSelected BIT, intProfileItemOptionItemID INT, bitAnswer BIT
,dteAnswerStart DATE, decWeight DEC(5,2))
INSERT INTO @CRWGroups (ClientID, intRegistrationWizardID, nvhProfileItemName, intProfileItemID, bitSelected, intProfileItemOptionItemID, bitAnswer, dteAnswerStart, decWeight)
SELECT crw.intClientID, crw.intRegistrationWizardID, pi.nvhProfileItemName, crwegipi.intProfileItemID, crwegipi.bitSelected, pioi.intProfileItemOptionItemID,
crwegipib.bitAnswer, crwegipid.dteAnswerStart, pioi.decWeight from tblClientRegistrationWizard crw
INNER JOIN tblClientRegistrationWizardElement crwe ON crwe.intClientRegistrationWizardID = crw.intClientRegistrationWizardID
INNER JOIN tblClientRegistrationWizardElementGroup crweg ON crweg.intClientRegistrationWizardElementID = crwe.intClientRegistrationWizardElementID
LEFT JOIN tblClientRegistrationWizardElementGroupItem_ProfileItem crwegipi ON crwegipi.intClientRegistrationWizardElementGroupID = crweg.intClientRegistrationWizardElementGroupID
LEFT JOIN tblClientRegistrationWizardElementGroupItem_ProfileItemOptionItem crwegipioi ON crwegipioi.intClientRegistrationWizardElementGroupItemProfileItemID = crwegipi.intClientRegistrationWizardElementGroupItemProfileItemID
LEFT JOIN tblClientRegistrationWizardElementGroupItem_ProfileItemBool crwegipib ON crwegipib.intClientRegistrationWizardElementGroupItemProfileItemID = crwegipi.intClientRegistrationWizardElementGroupItemProfileItemID
LEFT JOIN tblClientRegistrationWizardElementGroupItem_ProfileItemDate crwegipid ON crwegipid.intClientRegistrationWizardElementGroupItemProfileItemID = crwegipi.intClientRegistrationWizardElementGroupItemProfileItemID
LEFT join tblProfileItem pi ON pi.intProfileItemID = crwegipi.intProfileItemID
LEFT JOIN tblProfileItemOptionItem pioi ON pioi.intProfileItemOptionItemID = crwegipioi.intProfileItemOptionItemID
LEFT JOIN tblProfileItemBool pib ON pib.intProfileItemID = crwegipib.intClientRegistrationWizardElementGroupItemProfileItemID
WHERE crw.intRegistrationWizardID = 49 AND (crwegipi.bitSelected = 1 OR crwegipib.bitAnswer = 1)
SELECT(
SELECT
(SELECT DISTINCT
CASE WHEN cg.intProfileItemID IN (5174,4864) THEN '10'
WHEN cg.intProfileItemID = 4862 THEN '11' END
FROM @CRWGroups cg WHERE cg.intProfileItemID IN (5174,4864,4862) AND cg.ClientID = c.ClientID
FOR XML PATH ('EmpStat'),type)
,(SELECT DISTINCT
CASE WHEN cg.intProfileItemID IN (4864,5174,4862) THEN (SELECT ISNULL(CASE WHEN CONVERT(DATE, cg.dteAnswerStart) = '1900-01-01' THEN '' ELSE CONVERT(CHAR(10), cg.dteAnswerStart, 120) END, '')) END
FROM @CRWGroups cg WHERE cg.intProfileItemID IN (4864,5174,4862) AND cg.ClientID = c.ClientID
FOR XML PATH ('DateEmpStatApp'),type)
,(SELECT CASE WHEN cg.intProfileItemID IN (5180) THEN (SELECT 'LOU' AS 'ESMType', '2' AS 'ESMCode' FOR XML PATH ('EmploymentStatusMonitoring'),type) END)
,(SELECT CASE WHEN cg.intProfileItemID IN (4868) THEN (SELECT 'BSI' AS 'ESMType', '1' AS 'ESMCode' FOR XML PATH ('EmploymentStatusMonitoring'),type) END)
FROM @CRWGroups cg WHERE cg.ClientID = c.ClientID AND cg.intProfileItemID IN (5180,4868)
FOR XML PATH ('LearnerEmploymentStatus'),type)
FROM tblClient c
INNER JOIN tblClientRegistrationWizard crw ON crw.intClientID = c.ClientID WHERE crw.intRegistrationWizardID = 49
目前,此代码段会产生以下结果:
<LearnerEmploymentStatus>
<EmpStat>11</EmpStat>
<DateEmpStatApp>2017-06-05</DateEmpStatApp>
<EmploymentStatusMonitoring>
<ESMType>BSI</ESMType>
<ESMCode>1</ESMCode>
</EmploymentStatusMonitoring>
</LearnerEmploymentStatus>
<LearnerEmploymentStatus>
<EmpStat>11</EmpStat>
<DateEmpStatApp>2017-06-05</DateEmpStatApp>
<EmploymentStatusMonitoring>
<ESMType>LOU</ESMType>
<ESMCode>2</ESMCode>
</EmploymentStatusMonitoring>
</LearnerEmploymentStatus>
我希望以这种格式导出它:
<LearnerEmploymentStatus>
<EmpStat>11</EmpStat>
<DateEmpStatApp>2017-06-05</DateEmpStatApp>
<EmploymentStatusMonitoring>
<ESMType>BSI</ESMType>
<ESMCode>1</ESMCode>
</EmploymentStatusMonitoring>
<EmploymentStatusMonitoring>
<ESMType>LOU</ESMType>
<ESMCode>2</ESMCode>
</EmploymentStatusMonitoring>
</LearnerEmploymentStatus>
请注意,只有一个EmploymentStatus节点,其中包含两个EmploymentStatusMonitoring节点。