Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.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/1/visual-studio-2008/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 server 如何在SQL Server上使用XQuery按属性值对元素进行分组?_Sql Server_Xml_Group By_Xquery - Fatal编程技术网

Sql server 如何在SQL Server上使用XQuery按属性值对元素进行分组?

Sql server 如何在SQL Server上使用XQuery按属性值对元素进行分组?,sql-server,xml,group-by,xquery,Sql Server,Xml,Group By,Xquery,假设我在SQL Server中有一个表,其中包含具有内部联接的查询结果 以下是XQuery: select @code.query ( 'for $s in /root/row return <Foo Language="{data($s/@lang)}" Method="{data($s/@method)}" Returns="{data($s/@returnType)}"> <Bar ReferencedBy="{data($s/@callers)}" Static="{

假设我在SQL Server中有一个表,其中包含具有内部联接的查询结果

以下是XQuery:

select @code.query
(
'for $s in /root/row
return 
<Foo Language="{data($s/@lang)}" Method="{data($s/@method)}" Returns="{data($s/@returnType)}">
<Bar ReferencedBy="{data($s/@callers)}" Static="{data($s/@static)}" />
</Foo>'
)
选择@code.query
(
'对于/root/row中的$s
返回
'
)
其结果是:

<Foo Language="C#" Method="getFoos" Returns="FooCollection">
  <Bar ReferencedBy="Baz" Static="true" />
</Foo>
<Foo Language="C#" Method="getFoos" Returns="FooCollection">
  <Bar ReferencedBy="Bar" Static="false" />
</Foo>

事实上,我想要的是:

<Foo Language="C#" Method="getFoos" Returns="FooCollection">
  <Bar ReferencedBy="Baz" Static="true" />
  <Bar ReferencedBy="Bar" Static="false" />
</Foo>


为了避免使用LINQ和哈希表,使用XQuery执行此操作的最佳方法是什么?

在构建结果之前,您需要使用每种语言、方法和返回值枚举所有节点

for $lang in distinct-values(/root/row/@lang)
let $nodes := /root/row[@lang=$lang]
for $method in distinct-values($nodes/@method)
let $nodes := $nodes[@method=$method]
for $return in distinct-values($nodes/@returnType)
let $nodes := $nodes[@returnType=$returnType]
return
  <Foo Language="{$lang}"
       Method="{$method}"
       Returns="{$returnType}">
  {
    for $bar in $nodes
    return 
    <Bar ReferencedBy="{data($node/@callers)}"
         Static="{data($node/@static)}" />
  }
  </Foo>
不同值(/root/row/@lang)中的$lang的

让$nodes:=/root/row[@lang=$lang]
对于不同值中的$method($nodes/@method)
让$nodes:=$nodes[@method=$method]
对于不同值的$return($nodes/@returnType)
让$nodes:=$nodes[@returnType=$returnType]
返回
{
对于$bar,在$nodes中
返回
}

我自己不使用SQL Server,所以我不能保证这会起作用,但它是一个有效的XQuery解决方案。

看起来您需要在FOO标记中使用for循环来迭代要填充的条值。