简体   繁体   中英

Update table with a WITH and over partition diffuculty

I am trying to add a column in a table with a repeating series of linear numbers that stop at 287 and repeat. The code works in that the table updates, but all the rows are zero instead of the numbers I want. The With clause works since I can see it working when using a select with it, as something is up with the update.

WITH myupdate (myrownumber) 
 AS (SELECT ( Row_number() 
                OVER ( 
                  partition BY tmc, date 
                  ORDER BY tmc, date, epoch) - 1 ) AS myRowNumber 
     FROM   [dbo].[i40_2016_all]) 
--SELECT * FROM myUpdate 
UPDATE [dbo].[i40_2016_all] 
SET    mod_epoch = myrownumber 
FROM   myupdate 

My guess is that you are using SQL Server. I think that the syntax for what you want looks like this:

WITH myUpdate AS (
      SELECT a.*,
             ROW_NUMBER() over (PARTITION BY TMC, DATE ORDER BY TMC, DATE, EPOCH) - 1 AS myRowNumber
      FROM [dbo].[I40_2016_ALL] a
     )
UPDATE myUpdate
    SET MOD_EPOCH = myRowNumber;

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