简体   繁体   English

使用xquery对SQL Server 2005中的结果进行分组

[英]Grouping results in SQL Server 2005 using xquery

I am trying to extract data, to use in a rss feed for google base currently using SQL Server 2005. 我正在尝试提取数据,以在当前使用SQL Server 2005的google base的RSS提要中使用。

There a few problems we have, which i hope can be solved! 我们有一些问题,希望可以解决!

  1. namespaces 命名空间
  2. grouping 分组

My current sql is as follows: 我当前的sql如下:

SELECT [xml].query('
<Item xmlns:g="a">
<Title>{ data(*/*/*/Title) }</Title>
<g:id>{ sql:column("ReportingCode") }</g:id>
</Item>
')
FROM esh_xml 
where [Xml].value('(/*/*/*/Attributes/Attribute[@Description="Category"][text()="MasterMix"])[1]','nvarchar(2000)') is not null

as output with the current query I get many thousands of results rows ie 作为当前查询的输出,我得到了成千上万的结果行,即

<item xmlns:g="a"><title>blah blah</title><g:id>asdasd<g:id></item>

my desired output would be 我想要的输出是

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
      <channel>
            <title>Mastermixdigital.com Product Feed </title>
            <link> http://www.mysite.com/ </link>
            <description>mastermix latest release feed </description>
            <item xmlns:g="a">
                  <title>blah blah</title>
                  <g:id>asdasd<g:id>
            </item>
            <item xmlns:g="a">
                  <title>blah blah</title>
                  <g:id>asdasd<g:id>
            </item>
            <item xmlns:g="a">
                  <title>blah blah</title>
                  <g:id>asdasd<g:id>
            </item>
      </channel>
</rss>

Another thing I believe the execution of the query could be sped up by removing redundant nodes before searching through, and any tips on how i might go about this! 我相信另一件事是,可以通过在搜索之前删除冗余节点来加快查询的执行速度,以及有关如何进行此操作的任何提示!

Many thanks 非常感谢

I don't have your xml structure or tables so I created a sample that shows what you can do. 我没有您的xml结构或表,所以我创建了一个示例来展示您可以做什么。

Setup sample data, two rows with two items in xml for each row, one item in row one is duplicate with one item in row two. 设置示例数据,两行,每行xml中有两个项目,第一行中的一个重复,第二行中的一个重复。

declare @T table (ID int identity, [xml] xml)

insert into @T ([xml])
values ('
<root>
  <item>
    <id>1</id>
    <name>Name 1</name>
  </item>
  <item>
    <id>2</id>
    <name>Name 2</name>
  </item>
</root>
')

insert into @T ([xml])
values ('
<root>
  <item>
    <id>1</id>
    <name>Name 1</name>
  </item>
  <item>
    <id>3</id>
    <name>Name 3</name>
  </item>
</root>
')

Get id and name from all rows 从所有行获取ID和名称

select  
  r.i.value('id[1]', 'int') as id,
  r.i.value('name[1]', 'varchar(10)') as name
from @T as T
  cross apply T.[xml].nodes('root/item') r(i)

Result, four rows 结果,四行

id  name
1   Name 1
2   Name 2
1   Name 1
3   Name 3

Same query with duplicates removed 删除重复项的相同查询

select distinct 
  r.i.value('id[1]', 'int') as id,
  r.i.value('name[1]', 'varchar(10)') as name
from @T as T
  cross apply T.[xml].nodes('root/item') r(i)  

Result three rows 结果三行

id  name
1   Name 1
2   Name 2
3   Name 3

Join the rows back to xml using for xml path, root 使用xml路径(根)将行连接回xml

select distinct 
  r.i.value('id[1]', 'int') as id,
  r.i.value('name[1]', 'varchar(10)') as name
from @T as T
  cross apply T.[xml].nodes('root/item') r(i)  
for xml path('item'), root('root')

Result xml 结果xml

<root>
  <item>
    <id>1</id>
    <name>Name 1</name>
  </item>
  <item>
    <id>2</id>
    <name>Name 2</name>
  </item>
  <item>
    <id>3</id>
    <name>Name 3</name>
  </item>
</root>

With namespace 带名称空间

;with xmlnamespaces('a' as g)
select distinct 
  r.i.value('id[1]', 'int') as [g:id],
  r.i.value('name[1]', 'varchar(10)') as name
from @T as T
  cross apply T.[xml].nodes('root/item') r(i)  
for xml path('item'), root('root')

Result 结果

<root xmlns:g="a">
  <item>
    <g:id>1</g:id>
    <name>Name 1</name>
  </item>
  <item>
    <g:id>2</g:id>
    <name>Name 2</name>
  </item>
  <item>
    <g:id>3</g:id>
    <name>Name 3</name>
  </item>
</root>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM