3

鉴于以下 XML 示例和select将 xml 分解为关系的语句,我需要将 的第二列作为select类别的序数(即,在这种情况下,1 表示方向,2 表示颜色)。

注意:选择中的文字值 'rank()' 是一个占位符。我正在使用rank,但没有成功。

declare @x xml
set @x = '
    <root>
        <category>
            <item value="north"/>
            <item value="south"/>
            <item value="east"/>
            <item value="west"/>
        </category>
        <category>
            <item value="red"/>
            <item value="green"/>
            <item value="blue"/>
        </category>
    </root>'

select c.value('./@value', 'varchar(10)') as "ItemValue", 
       'rank()' as "CategoryNumber"
from @x.nodes('//item') as t(c)
4

4 回答 4

2

Jacob Sebastian 在他的博文中也提出了一个有趣的解决方案:

XQuery 实验室 23 - 检索元素的值和位置

根据 Jacob 的建议,我可以将您的查询改写为:

SELECT
    x.value('@value','VARCHAR(10)') AS 'ItemValue',        
    p.number as 'CategoryNumber'
FROM
    master..spt_values p
CROSS APPLY 
    @x.nodes('/root/category[position()=sql:column("number")]/item') n(x) 
WHERE
    p.type = 'p'

我得到了想要的输出:

ItemValue   CategoryNumber
---------   --------------
north           1
south           1
east            1
west            1
red             2
green           2
blue            2

不幸的是,像position()orfn:id()函数这样的更明显的解决方案似乎 a) 在 SQL Server 中工作或 b) 在 SQL Server 中根本不受支持:-(

希望这可以帮助

马克

于 2009-10-07T20:37:57.677 回答
1

可能是这样的:你得到每个类别的第一个元素并将其用作 id。

这:

select c.value('./@value', 'varchar(10)') as "ItemValue", 
    c.value('../item[1]/@value', 'varchar(10)') as "CategoryNumber"
from @x.nodes('//item') as t(c)

回报:

Item Value | CategoryNumber
---------------------------
north      | north
south      | north
east       | north
west       | north
red        | red
green      | red
blue       | red

然后就

select c.value('./@value', 'varchar(10)') as "ItemValue", 
   RANK() OVER (ORDER BY c.value('../item[1]/@value', 'varchar(10)')) as "CategoryNumber"
from @x.nodes('//item') as t(c)

但是它返回:

Item Value | CategoryNumber
---------------------------
north      | 1
south      | 1
east       | 1
west       | 1
red        | 5
green      | 5
blue       | 5

但它仍然领先一步。

于 2009-10-07T16:50:49.147 回答
1

你不能用它position()来产生输出(为什么??),但你可以把它用作 XPath 过滤器:

 with numbers (n) as (
  select 1
  union all select 2
  union all select 3
  union all select 4
  union all select 5)
 select i.x.value('@value', 'varchar(10)') as [ItemValue],
    n.n as [rank]
  from numbers n
  cross apply @x.nodes('/root/category[position()=sql:column("n.n")]') as c(x)
  cross apply c.x.nodes('item') as i(x);

您可以使用实数表来获得更高的排名。对于单个文档中的大量类别而言,效率并不高,但对于中等数量(数十、数百)将工作得很好。

于 2009-10-07T17:45:59.597 回答
0

与 Lukasz 的回答类似,我能够通过以下方式达到预期的结果:

SELECT
  I.Item_Instance.value('@value','VARCHAR(10)') AS Item_Value,
  DENSE_RANK() OVER (ORDER BY C.Category_Instance) AS Category_Ordinal  
FROM @x.nodes('/root') AS R(Root_Instance)
CROSS APPLY R.Root_Instance.nodes('category') AS C(Category_Instance)
CROSS APPLY C.Category_Instance.nodes('item') AS I(Item_Instance);

它返回:

Item_Value Category_Ordinal
---------- --------------------
north      1
south      1
east       1
west       1
red        2
green      2
blue       2
于 2017-05-31T22:00:31.180 回答