我得到了这个包含 TODO 项目的任务表。我们在单个存储过程中使用单独的查询来检索待办事项和已完成、待处理任务的计数,即使它是从同一个表中查询的。这是查询,
select
TaskName 'Task/TaskName',
CASE IsDone WHEN '1' THEN 'True' ELSE 'False' END 'Task/IsDone',
(
SELECT COUNT(*) FROM Tasks WHERE IsDone = '1'
) 'CompletedCount'
FROM Tasks FOR XML PATH('Tasks')
这是输出
'<Tasks>
<Task>
<TaskName>Write a email to Mayor<TaskName>
<IsDone>True</IsDone>
<CompletedCount>2<CompletedCount>
</Task>
</Tasks>'
CompletedCount
每个任务中都存在这是不必要的,无论如何我也可以查询计数而无需明确编写 SELECT COUNT(*) FROM Tasks WHERE IsDone = '1'
我如何获得如下输出
'<Tasks>
<CompletedCount>2<CompletedCount>
<Task>
<TaskName>Write a email to Mayor<TaskName>
<IsDone>True</IsDone>
</Task>
<Task>
<TaskName>Organize Campaign for website<TaskName>
<IsDone>False</IsDone>
</Task>
</Tasks>'