Skip to content
Advertisement

Update column with below query?

How do I write this query to update a column based on the case statement?

UPDATE Table
SET Form = CASE 
              WHEN (cnt - rownum < cnt % NumberUp) 
                 THEN 0 
                 ELSE  floor((rowNum - 1) / numberUp) + 1 
           END AS form
ORDER BY Quantity

Advertisement

Answer

As commented, you query looks pretty good, apart from these:

  • the ORDER BY clause does not make sense; an UPDATE query does not return any record, so ordering is out of scope

  • for the same reason, you don’t have to alias the updated column

Consider:

UPDATE Table
SET Form = CASE 
    WHEN (cnt - rownum < cnt % NumberUp) THEN 0 
    ELSE  floor((rowNum - 1) / numberUp) + 1 
END
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement