Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取SQL中每个组的XML元素计数_Xml_Sql Server 2005_Grouping - Fatal编程技术网

获取SQL中每个组的XML元素计数

获取SQL中每个组的XML元素计数,xml,sql-server-2005,grouping,Xml,Sql Server 2005,Grouping,我有一个XML列,如下所示: DECLARE @str AS VARCHAR(8000) SET @str = '<Root xmlns="http://myurl.com/services/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <Notifications> <Notification> <N

我有一个XML列,如下所示:

DECLARE @str AS VARCHAR(8000)

SET @str = '<Root xmlns="http://myurl.com/services/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
           <Notifications>
               <Notification>
                  <NotificationDate>2012-08-16</NotificationDate>
                  <Scopes>
                    <NotificationScope>
                        <Code>a</Code>
                    </NotificationScope>  
                    <NotificationScope>
                        <Code>b</Code> 
                    </NotificationScope>
                  </Scopes>
                </Notification>
                <Notification>
                  <NotificationDate>2012-08-20</NotificationDate>
                  <Scopes>
                    <NotificationScope>
                        <Code>a</Code>
                    </NotificationScope>
                  </Scopes>
                </Notification>
            </Notifications></Root>'
我希望能够获得每个通知的NotificationScope元素数。举个例子,我在寻找这样的东西

Notification Date                   Count
 2012-08-16                          2
 2012-08-20                          1
事实上,实际上,由于这些数据是从数据库列获取的,我真正感兴趣的是返回那些具有任何通知日期且计数大于1的记录

到目前为止,我所能想到的只是以下几点,但这只是给了我一个数字

declare @xmlvar XML;
set @xmlvar = (SELECT cast(@str AS XML))
;WITH XMLNAMESPACES('http://myurl.com/services/' AS p)
SELECT * FROM (
select @xmlvar.value('count(/p:Root/p:Notifications/p:Notification/p:Scopes/p:NotificationScope)', 'INT') AS 'NotificationScopeCount',
       @xmlvar.value('count(/p:Root/p:Notifications/p:Notification/p:NotificationDate)', 'INT') AS 'NotificationDateCount'
 ) a WHERE NotificationScopeCount > NotificationDateCount

但理想情况下,我希望能够得到相关的日期也。如果这是一种可怕的方式,请原谅,我以前在SQL中很少使用XML数据类型

您可以尝试使用以下内容:

;WITH XMLNAMESPACES('http://myurl.com/services/' AS p)
SELECT
    NotificationDate = Notif.value('(p:NotificationDate)[1]', 'DATE'),
    CodeCount = Notif.value('count(p:Scopes/p:NotificationScope/p:Code)', 'int')
FROM
    @xmlvar.nodes('/p:Root/p:Notifications/p:Notification') AS Tbl(Notif)
给我一个输出:


您可以尝试使用以下内容:

;WITH XMLNAMESPACES('http://myurl.com/services/' AS p)
SELECT
    NotificationDate = Notif.value('(p:NotificationDate)[1]', 'DATE'),
    CodeCount = Notif.value('count(p:Scopes/p:NotificationScope/p:Code)', 'int')
FROM
    @xmlvar.nodes('/p:Root/p:Notifications/p:Notification') AS Tbl(Notif)
给我一个输出:


谢谢你的帮助。NotificationDate列中的[1]是否作为值的聚合类型?@MrMoose:no,
[1]
只选择该XPath定义的XML节点列表中的第一个(可能是多个)条目。由于您的
节点只包含一个
(我假设是这样),这就是您想要的-您可以从通知节点获取通知日期。我这里的代码没有进行任何聚合(目前)-如果需要,您必须聚合自己(例如,将其包装到CTE中,然后在日期列上执行
分组方式)。谢谢你。我已经了解了如何将cte与xmlnamespaces相结合,以了解如何实现这一点:)谢谢。NotificationDate列中的[1]是否作为值的聚合类型?@MrMoose:no,
[1]
只选择该XPath定义的XML节点列表中的第一个(可能是多个)条目。由于您的
节点只包含一个
(我假设是这样),这就是您想要的-您可以从通知节点获取通知日期。我这里的代码没有进行任何聚合(目前)-如果需要,您必须聚合自己(例如,将其包装到CTE中,然后在日期列上执行
分组方式)。谢谢你。我已经了解了如何将cte与xmlnamespaces相结合,以了解如何实现这一点:)