以下に示すように、結果をグループ化/ネストしようとすると問題が発生します。
どんな助けでも大歓迎です。
必要な 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 出力 プロセス 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