我在尝试对结果进行分组/嵌套时遇到问题,如下所示。
非常感谢任何帮助。
必需的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
答案 0 :(得分:5)
将group by
添加到您的查询中:
select
m1.TypeCause as [@id1],
(
select
m2.MTypeNumber as [@id],
(
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 dbo.CombineDataSets as m3
where
m3.LossCause = 'C_Only' and
m3.MTypeNumber = m2.MTypeNumber
m3.Subject = m2.Subject and
m3.date = m2.date
for xml path('AuditTrailEntry'), type
)
from dbo.CombineDataSets as m2
where
m2.TypeCause = 'Contents Only' and
m2.MTypeNumber = m1.MTypeNumber
m2.Subject = m1.Subject
m2.date = m1.date
for xml path('ProcessInstance'), type
)
from dbo.CombineDataSets as m1
where m1.TypeCause='C_Only'
group by m1.TypeCause, m1.date, m1.MTypeNumber, m1.Subject
order by m1.TypeCause
for xml path('Process'), root('WorkflowLog')
答案 1 :(得分:1)
Roman是正确的,问题出在我的分组上,但也有where语句加入。
select
m1.TypeCause as [@id1],
(
SELECT m2.MTypeNumber as [@id],
(
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 as m3
where
m3.MTypeNumber=m2.MTypeNumber
and m3.TypeCause=m2.TypeCause
for xml path('AuditTrailAudit'),type
)
from #CombinedDataSets as m2
where
m2.TypeCause='C_Only'
group by m2.TypeCause, m2.MTypeNumber
for XML path('ProcessInstance'),type
)
from #CombinedDataSets as m1
where
m1.TypeCause='Contents Only'
group by
m1.TypeCause
For XML path('Process'),Root('WorkflowLog')