简体   繁体   中英

rownumber() function to count adjacent cells

I need to update contact person data using DTW (can't do directly with sql script). 'CardCode' is the key field. 'CntctCode' is the record but you cannot update on the field. You must import use a number starting with 0 and increasing for each record. I want the query to add a field starting ay 0 and increasing for each new CardCode record.

I have been successful with row_number() over order function but only for 1 CardCode record. I need it to work on a larger set of CardCodes.

select  row_number() over (order by CntctCode) as PositionInTable
,       *
from OCPR



select  row_number() over (order by T0.CntctCode) as PositionInTable
,       *
from    OCPR T0 
    WHERE T0.CardCode = 'C00001'



PositionInTable CntctCode   CardCode
1   12101   C00001
2   12102   C00001
3   12103   C00001
4   12315   C00001
5   12696   C00001
6   13097   C00001
7   13098   C00001
8   13328   C00001
9   13408   C00001
10  13628   C00001
11  13661   C00001
12  13662   C00001
13  14634   C00001

you can use partition by

select  row_number() over (partition by CardCode order by CntctCode) as PositionInTable
,       *
from OCPR

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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