简体   繁体   中英

Update a column based on record numbers

I'm creating a column called "Deciles". I need each decile to populate based on the range of the row numbers. So 'Decile 1' would be between rows 1 and 364000. The syntax I've used latest has not worked. Please keep in mind I am not a programmer. (SQL Server 2008 is the environment I'm using). HELP!!

update dbo.August_Deciles
set Deciles = 'Decile 1'
Row_Number() over (Order By Row_Number())
between Row_Number(1) and  Row_Number(326424) 

You could try this CTE:

WITH CTE AS
(
    SELECT Deciles,
           RN = ROW_NUMBER() OVER (ORDER BY Deciles ASC)
    FROM dbo.August_Deciles
)
UPDATE CTE SET Deciles = 'Decile ' + CAST(RN as int)

You should replace ORDER BY Deciles ASC with a meaningful order.

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