SQL Server 2008 FOR XML 路径帮助

发布于 2024-11-09 05:52:02 字数 2307 浏览 3 评论 0原文

我有一张历史记录表。 [添加] 一个字段具有日期时间数据类型。我想要做的是选择用户 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天煞孤星 2024-11-16 05:52:02

试试这个:

/*  INIT  */
DECLARE @ActivityHistory TABLE (id int, bar VARCHAR(3), bam datetime)
INSERT INTO @ActivityHistory 
             SELECT  id='10000', bar='zoo', bam='2011-05-24 03:32:57' 
       UNION SELECT  id='10001', bar='zoo', bam='2011-05-24 03:31:57' 
       UNION SELECT  id='10002', bar='zoo', bam='2011-05-24 03:28:57' 
       UNION SELECT  id='10003', bar='zoo', bam='2011-05-24 03:21:57' 
       UNION SELECT  id= '9990', bar='zoo', bam='2011-05-23 03:32:57' 
       UNION SELECT  id= '9989', bar='zoo', bam='2011-05-23 03:31:57' 
       UNION SELECT  id= '9988', bar='zoo', bam='2011-05-23 03:28:57' 
       UNION SELECT  id= '9987', bar='zoo', bam='2011-05-23 03:21:57' 
       UNION SELECT  id= '9900', bar='zoo', bam='2011-05-22 03:32:57' 
       UNION SELECT  id= '9899', bar='zoo', bam='2011-05-22 03:31:57' 
       UNION SELECT  id= '9898', bar='zoo', bam='2011-05-22 03:28:57' 
       UNION SELECT  id= '9897', bar='zoo', bam='2011-05-22 03:21:57' 

/*  QUERY  */
;WITH 
resALL AS ( SELECT *
      , foo = DATENAME(weekday, bam)+', '+ CONVERT(VARCHAR(30), bam, 107) 
      , food = CONVERT(VARCHAR(10), bam, 121) 
    FROM @ActivityHistory AS Activity
  )
, resD AS ( SELECT DISTINCT foo, food FROM resALL 
  )

SELECT 
 Activities.foo
 , (
    SELECT id, bar, bam 
    FROM resALL AS Activity 
    WHERE foo = Activities.foo 
    ORDER BY bam desc 
    FOR XML AUTO, TYPE
   )
FROM resD AS Activities
ORDER BY Activities.food DESC
FOR XML AUTO, TYPE, ROOT ('ActivityHistory')

/*  OUTPUT
<ActivityHistory>
  <Activities foo="Tuesday, May 24, 2011">
    <Activity id="10000" bar="zoo" bam="2011-05-24T03:32:57" />
    <Activity id="10001" bar="zoo" bam="2011-05-24T03:31:57" />
    <Activity id="10002" bar="zoo" bam="2011-05-24T03:28:57" />
    <Activity id="10003" bar="zoo" bam="2011-05-24T03:21:57" />
  </Activities>
  <Activities foo="Monday, May 23, 2011">
    <Activity id="9990" bar="zoo" bam="2011-05-23T03:32:57" />
    <Activity id="9989" bar="zoo" bam="2011-05-23T03:31:57" />
    <Activity id="9988" bar="zoo" bam="2011-05-23T03:28:57" />
    <Activity id="9987" bar="zoo" bam="2011-05-23T03:21:57" />
  </Activities>
  <Activities foo="Sunday, May 22, 2011">
    <Activity id="9900" bar="zoo" bam="2011-05-22T03:32:57" />
    <Activity id="9899" bar="zoo" bam="2011-05-22T03:31:57" />
    <Activity id="9898" bar="zoo" bam="2011-05-22T03:28:57" />
    <Activity id="9897" bar="zoo" bam="2011-05-22T03:21:57" />
  </Activities>
</ActivityHistory>
*/

Try this:

/*  INIT  */
DECLARE @ActivityHistory TABLE (id int, bar VARCHAR(3), bam datetime)
INSERT INTO @ActivityHistory 
             SELECT  id='10000', bar='zoo', bam='2011-05-24 03:32:57' 
       UNION SELECT  id='10001', bar='zoo', bam='2011-05-24 03:31:57' 
       UNION SELECT  id='10002', bar='zoo', bam='2011-05-24 03:28:57' 
       UNION SELECT  id='10003', bar='zoo', bam='2011-05-24 03:21:57' 
       UNION SELECT  id= '9990', bar='zoo', bam='2011-05-23 03:32:57' 
       UNION SELECT  id= '9989', bar='zoo', bam='2011-05-23 03:31:57' 
       UNION SELECT  id= '9988', bar='zoo', bam='2011-05-23 03:28:57' 
       UNION SELECT  id= '9987', bar='zoo', bam='2011-05-23 03:21:57' 
       UNION SELECT  id= '9900', bar='zoo', bam='2011-05-22 03:32:57' 
       UNION SELECT  id= '9899', bar='zoo', bam='2011-05-22 03:31:57' 
       UNION SELECT  id= '9898', bar='zoo', bam='2011-05-22 03:28:57' 
       UNION SELECT  id= '9897', bar='zoo', bam='2011-05-22 03:21:57' 

/*  QUERY  */
;WITH 
resALL AS ( SELECT *
      , foo = DATENAME(weekday, bam)+', '+ CONVERT(VARCHAR(30), bam, 107) 
      , food = CONVERT(VARCHAR(10), bam, 121) 
    FROM @ActivityHistory AS Activity
  )
, resD AS ( SELECT DISTINCT foo, food FROM resALL 
  )

SELECT 
 Activities.foo
 , (
    SELECT id, bar, bam 
    FROM resALL AS Activity 
    WHERE foo = Activities.foo 
    ORDER BY bam desc 
    FOR XML AUTO, TYPE
   )
FROM resD AS Activities
ORDER BY Activities.food DESC
FOR XML AUTO, TYPE, ROOT ('ActivityHistory')

/*  OUTPUT
<ActivityHistory>
  <Activities foo="Tuesday, May 24, 2011">
    <Activity id="10000" bar="zoo" bam="2011-05-24T03:32:57" />
    <Activity id="10001" bar="zoo" bam="2011-05-24T03:31:57" />
    <Activity id="10002" bar="zoo" bam="2011-05-24T03:28:57" />
    <Activity id="10003" bar="zoo" bam="2011-05-24T03:21:57" />
  </Activities>
  <Activities foo="Monday, May 23, 2011">
    <Activity id="9990" bar="zoo" bam="2011-05-23T03:32:57" />
    <Activity id="9989" bar="zoo" bam="2011-05-23T03:31:57" />
    <Activity id="9988" bar="zoo" bam="2011-05-23T03:28:57" />
    <Activity id="9987" bar="zoo" bam="2011-05-23T03:21:57" />
  </Activities>
  <Activities foo="Sunday, May 22, 2011">
    <Activity id="9900" bar="zoo" bam="2011-05-22T03:32:57" />
    <Activity id="9899" bar="zoo" bam="2011-05-22T03:31:57" />
    <Activity id="9898" bar="zoo" bam="2011-05-22T03:28:57" />
    <Activity id="9897" bar="zoo" bam="2011-05-22T03:21:57" />
  </Activities>
</ActivityHistory>
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文