SQL Server 2008 FOR XML 路径帮助
我有一张历史记录表。 [添加] 一个字段具有日期时间数据类型。我想要做的是选择用户 20 个最近的记录,然后按从 [added] 字段派生的字符串对它们进行分组。假设其值为 2011-05-24 03:32:57.353
,字符串为 'Tuesday, May 24, 2011'
。我想按该字符串对记录进行分组,然后按实际时间对子节点进行排序。我还想要一些自定义 XML 输出。
<ActivityHistory>
<Actvities foo="Tuesday, May 24, 2011">
<Activity id="10000" bar="zoo" bam="2011-05-24 03:32:57.353" />
<Activity id="10001" bar="zoo" bam="2011-05-24 03:31:57.353" />
<Activity id="10002" bar="zoo" bam="2011-05-24 03:28:57.353" />
<Activity id="10003" bar="zoo" bam="2011-05-24 03:21:57.353" />
</Activities>
<Actvities foo="Monday, May 23, 2011">
<Activity id="9990" bar="zoo" bam="2011-05-23 03:32:57.353" />
<Activity id="9989" bar="zoo" bam="2011-05-23 03:31:57.353" />
<Activity id="9988" bar="zoo" bam="2011-05-23 03:28:57.353" />
<Activity id="9987" bar="zoo" bam="2011-05-23 03:21:57.353" />
</Activities>
<Actvities foo="Sunday, May 22, 2011">
<Activity id="9900" bar="zoo" bam="2011-05-22 03:32:57.353" />
<Activity id="9899" bar="zoo" bam="2011-05-22 03:31:57.353" />
<Activity id="9898" bar="zoo" bam="2011-05-22 03:28:57.353" />
<Activity id="9897" bar="zoo" bam="2011-05-22 03:21:57.353" />
</Activities>
</ActivityHistory>
此负载始终只有 0-20 条记录。可能它永远是 20。
到目前为止我的查询看起来像这样。
SELECT TOP 20
fnHistoryGroupingText(Added) [@foo]
FROM ActivityHistory
WHERE MricId = 1
GROUP BY fnHistoryGroupingText(Added)
FOR XML PATH ('Activities'), ROOT ('ActivityHistory')
它生成的 XML 类似于我正在寻找的内容。
<ActivityHistory>
<Activities foo="Friday, May 20, 2011" />
<Activities foo="Monday, May 23, 2011" />
<Activities foo="Saturday, May 21, 2011" />
<Activities foo="Sunday, May 22, 2011" />
<Activities foo="Tuesday, May 24, 2011" />
</ActivityHistory>
请注意,它没有按日期排序并且缺少子节点。我希望它们按时间倒序排列。我故意从查询中排除了一些字段,因为此时在查询中我正在获得我最终想要的结构。当我介绍其他字段时,XML 就差得很远了。分组文本是 varchar 并且不能很好地转换为日期。我可以以某种方式使用 [added] 字段,但是当我将其包含在查询中时,它会破坏我的分组。谁能指出我纠正这些问题的正确方向? [A] 正确编写查询,并且 [B] 向我展示如何正确输出我正在查找的 XML。
I have a table of history records. One field [added] has a datetime data type. What I'm trying to do is select a users 20 most recent records, but then group them by a string derived from the [added] field. Say its value was 2011-05-24 03:32:57.353
, the string is 'Tuesday, May 24, 2011'
. I want to group the records by that string, but then order the child nodes by the actual time. I also want a bit of custom XML output.
<ActivityHistory>
<Actvities foo="Tuesday, May 24, 2011">
<Activity id="10000" bar="zoo" bam="2011-05-24 03:32:57.353" />
<Activity id="10001" bar="zoo" bam="2011-05-24 03:31:57.353" />
<Activity id="10002" bar="zoo" bam="2011-05-24 03:28:57.353" />
<Activity id="10003" bar="zoo" bam="2011-05-24 03:21:57.353" />
</Activities>
<Actvities foo="Monday, May 23, 2011">
<Activity id="9990" bar="zoo" bam="2011-05-23 03:32:57.353" />
<Activity id="9989" bar="zoo" bam="2011-05-23 03:31:57.353" />
<Activity id="9988" bar="zoo" bam="2011-05-23 03:28:57.353" />
<Activity id="9987" bar="zoo" bam="2011-05-23 03:21:57.353" />
</Activities>
<Actvities foo="Sunday, May 22, 2011">
<Activity id="9900" bar="zoo" bam="2011-05-22 03:32:57.353" />
<Activity id="9899" bar="zoo" bam="2011-05-22 03:31:57.353" />
<Activity id="9898" bar="zoo" bam="2011-05-22 03:28:57.353" />
<Activity id="9897" bar="zoo" bam="2011-05-22 03:21:57.353" />
</Activities>
</ActivityHistory>
This payload will always only have 0-20 records. Likely it will always be 20.
So far my query looks like this.
SELECT TOP 20
fnHistoryGroupingText(Added) [@foo]
FROM ActivityHistory
WHERE MricId = 1
GROUP BY fnHistoryGroupingText(Added)
FOR XML PATH ('Activities'), ROOT ('ActivityHistory')
It produces XML similar to what I'm looking for.
<ActivityHistory>
<Activities foo="Friday, May 20, 2011" />
<Activities foo="Monday, May 23, 2011" />
<Activities foo="Saturday, May 21, 2011" />
<Activities foo="Sunday, May 22, 2011" />
<Activities foo="Tuesday, May 24, 2011" />
</ActivityHistory>
Notice it is not date sorted and the lack of child nodes. I would like them sorted in reverse chronological order. I purposely excluded some fields from the query as at this point in the query I'm sort of getting the structure I ultimately want. When I introduce the other fields, the XML is way off. Grouping text is a varchar and doesn't covert nicely to a date. I can use the [added] field somehow, but when I include it in the query, it busts my grouping. Can anyone point me in the right direction to correct these problems? [A] Write the query correctly, and [B] Show me how to correctly output the XML I'm looking for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
Try this: