我正在尝试创建一个类似于序列号的东西,每行迭代一次,并在列shopID更改时重置。但我想使用变量。
select
@Counter --what to do here?
,@data as Data
,case
when (ShopID.N.value('.', 'decimal(5,0)') = -1) then NULL
else ShopID.N.value('.', 'decimal(5,0)')
end as ShopID
from @xmlRes.nodes('/Root/ResponseData/response/shopItems') as S(N)
outer apply S.N.nodes('recordNumber') as ShopID(N)
我需要得到这样的东西:
Counter | Data | ShopID
--------------------------------------
1 | Sample | 1
2 | Sample | 1
3 | Sample | 1
1 | Sample | 2
2 | Sample | 2
3 | Sample | 2
4 | Sample | 2
1 | Sample | 3
答案 0 :(得分:3)
select
ROW_NUMBER() OVER (PARTITION BY ShopID ORDER BY Data) as Counter,
,@data as Data
,case
when (ShopID.N.value('.', 'decimal(5,0)') = -1) then NULL
else ShopID.N.value('.', 'decimal(5,0)')
end as ShopID
from @xmlRes.nodes('/Root/ResponseData/response/shopItems') as S(N)
outer apply S.N.nodes('recordNumber') as ShopID(N)
答案 1 :(得分:1)
row_number() over (partition by ShopID)
如果您希望按特定顺序分配序列号,还应在分区后添加order by子句。