Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update SQL column using Rank() function

Tags:

sql

sql-server

I have a table with existing data. For each unique value in the first column of this table, we have a column that is supposed to be in sequential order, but this table has gotten out of order. I want to run a SQL statement that will put this second column back in order. I was able to see the results I want with this SQL:

select FORMULA_ID, ATTRIB_CODE, ATTRIB_VAL, ATTRIB_ORDER,
rank() over (partition by formula_id order by attrib_code, attrib_val) AS WANT_THIS
from ATTRIB

Which yields:

 FORMULA_ID  ATTRIB_CODE          ATTRIB_VAL       ATTRIB_ORDER WANT_THIS
 ----------- -------------------- ---------------- ------------ ---------
 2791        C_BRAND              ROMAN HOLIDAY    3            1
 2791        C_ENDUSE             DINNER           4            2
 2791        C_ENDUSE             SNACK            6            3
 2791        C_ENDUSER            10-17            7            4
 2791        C_PRODTYPE           SALAD            13           5
 2791        C_RELIG              ANY              14           6
 2821        C_ALLERGEN           PEANUT           1            1
 2821        C_ALLERGEN           SOY              2            2
 2821        C_BRAND              ROMAN HOLIDAY    1            3
 2821        C_ENDUSE             DINNER           1            4

As you can see, the WANT_THIS column orders the rows and resets to 1 when it gets to a new FORMULA_ID. But I don't know how to convert this into an UPDATE statement that will actually put the value in WANT_THIS into the column ATTRIB_ORDER. Is there a way to convert the SQL above into an UPDATE statement?

like image 561
Bert Cushman Avatar asked Jun 13 '16 20:06

Bert Cushman


People also ask

What does RANK () do in SQL?

Returns the rank of each row within the partition of a result set. The rank of a row is one plus the number of ranks that come before the row in question. ROW_NUMBER and RANK are similar. ROW_NUMBER numbers all rows sequentially (for example 1, 2, 3, 4, 5).

How do you update a list of values in a column in SQL?

The UPDATE command in SQL is used to modify or change the existing records in a table. If we want to update a particular value, we use the WHERE clause along with the UPDATE clause. If you do not use the WHERE clause, all the rows will be affected.

Can we use top with update in SQL?

We can use TOP Clause in a SQL Update statement as well to restrict the number of rows for an update. Essentially, it is a combination of the select statement and update. In the following query, we use the TOP clause in the select statement and update the values in the [HumanResources. Employee] table.


1 Answers

This is one way:

WITH CTE AS
(
    SELECT  FORMULA_ID, 
            ATTRIB_CODE, 
            ATTRIB_VAL, 
            ATTRIB_ORDER,
            RANK() OVER (PARTITION BY formula_id 
                         ORDER BY attrib_code, attrib_val) AS WANT_THIS
    FROM ATTRIB
)
UPDATE CTE
SET ATTRIB_ORDER = WANT_THIS;
like image 80
Lamak Avatar answered Sep 26 '22 19:09

Lamak