Shredded xml rank

Given the following XML example and the operator selectthat breaks the xml into a relation, I need a second column selectas the category ordinal (i.e. 1 for directions and 2 for colors in this case).

Note. The literal value 'rank ()' in the selection element remains a placeholder. I poked with help rank, but without success.

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)
+3
source share
4 answers

Jacob Sebastian also has an interesting solution presented on his blog:

XQuery Lab 23 - Retrieving Values ​​and Position of Elements

:

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() fn:id(), , ) SQL Server b) SQL Server: - (

,

+2

, : 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

.

+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);

. , (, ) .

+1

, :

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
0

Source: https://habr.com/ru/post/1719532/


All Articles