我在尝试对结果进行分组/嵌套时遇到问题,如下所示。
任何帮助将不胜感激。
所需的 XML 输出
<WorkflowLog>
<Process id="Type1">
<ProcessInstance id="M11111">
<AuditTrailEntry>
<Data>
<Attribute name="TypeSubCause">EMB</Attribute>
</Data>
<WorkflowModelElement>C_Created</WorkflowModelElement>
<EventType>Start</EventType>
<TimeStamp>2013-10-02T10:00:33</TimeStamp>
<Originator>C_Team</Originator>
</AuditTrailEntry>
<AuditTrailEntry>
<Data>
<Attribute name="TypeSubCause">EMB</Attribute>
</Data>
<WorkflowModelElement>ITPSPotential</WorkflowModelElement>
<EventType>Start</EventType>
<TimeStamp>2013-10-03T09:15:32+10.00</TimeStamp>
<Originator>R_Team</Originator>
</AuditTrailEntry>
</ProcessInstance>
</Process>
</WorkflowLog>
我的 SQL 的实际 XML 输出 Process Id 和 ProcessInstance ID 多次显示相同的 Id
<WorkflowLog>
<Process id="Type1">
<ProcessInstance id="M11111">
<AuditTrailEntry>
<Data>
<Attribute name="TypeSubCause">EMBt</Attribute>
</Data>
<WorkflowModelElement>c_Created</WorkflowModelElement>
<EventType>Start</EventType>
<TimeStamp>2013-10-02T10:00:33+10.00</TimeStamp>
<Originator>C_Team</Originator>
</AuditTrailEntry>
</ProcessInstance>
</Process>
<Process id="Type1">
<ProcessInstance id="M11111">
<AuditTrailEntry>
<Data>
<Attribute name="TypeSubCause">EMB</Attribute>
</Data>
<WorkflowModelElement>ITPSPotential</WorkflowModelElement>
<EventType>Start</EventType>
<TimeStamp>2013-10-03T09:15:32+10.00</TimeStamp>
<Originator>R_Team</Originator>
</AuditTrailEntry>
</ProcessInstance>
</Process>
</WorkflowLog>
生成这个的 SQL 是
SELECT '@id' = m1.TypeCause ,
( SELECT '@id' = m2.MTypeNumber ,
( SELECT 'TypeSubCause' AS [Data/Attribute/@name] ,
m3.[TypeSubCause] AS [Data/Attribute/*] ,
m3.[Subject] AS [WorkflowModelElement] ,
'Start' AS [EventType] ,
m3.[date] AS [TimeStamp] ,
m3.[AssignedGroup] AS [Originator]
FROM #CombinedDataSets M3
WHERE m3.TypeCause = 'C_Only'
AND m2.MTypeNumber = m3.MTypeNumber
AND m2.Subject = m3.Subject
AND m2.date = m3.date
FOR
XML PATH('AuditTrailEntry') ,
TYPE
)
FROM #CombinedDataSets m2
WHERE m2.TypeCause = 'C_Only'
AND m1.MTypeNumber = m2.MTypeNumber
AND m1.Subject = m2.Subject
AND m1.date = m2.date
FOR
XML PATH('ProcessInstance') ,
TYPE
)
FROM #CombinedDataSets m1
WHERE m1.TypeCause = 'C_Only'
ORDER BY m1.TypeCause ,
m1.MTypeNumber ,
m1.date
FOR XML PATH('Process') ,
ROOT('WorkflowLog')
添加 - 示例数据
CREATE TABLE #CombinedDataSets
(
[MTypeNumber] VARCHAR(10) ,
subject VARCHAR(25) ,
[Date] DATETIME ,
[AssignedGroup] VARCHAR(25) ,
[TypeCause] VARCHAR(25) ,
[TypeSubCause] VARCHAR(25)
)
INSERT INTO #CombinedDataSets
( [MTypeNumber] ,
[subject] ,
[Date] ,
[AssignedGroup] ,
[TypeCause] ,
[TypeSubCause]
)
SELECT 'M11111' ,
'C_Created' ,
'2013-10-02 10:00:33' ,
'CA' ,
'C_Only' ,
'EMB'
UNION ALL
SELECT 'M11111' ,
'ITPSPotential' ,
'2013-10-03 09:15:32' ,
'ALLPIC' ,
'C_Only' ,
'EMB'
SELECT *
FROM #CombinedDataSets
MTypeNumber subject Date AssignedGroup TypeCause TypeSubCause
M11111 C_Created 2013-10-02 10:00:33.000 CA C_Only EMB
M11111 ITPSPotential 2013-10-03 09:15:32.000 ALLPIC C_Only EMB