Update column of a table based on array of varchar in PostgreSQL

curiosa

I have a table to which i have added a column varchar colorcode.

The table already has many rows. state of table after adding colorcode column is

id   name               location     colorcode
121  Royal Challengers  Bangalore     
122  Sun Risers         Hyderabad
123  Dare Devils        Delhi
124  Gujrat Lions       Ahmadabad

I have a array of color codes

 ["#FF8484", "#FF82A9", "#FA82FF", "#C682FF", "#8782FF"]

For each row in the table I must update the colorcode column by matching index of array with (row_number() - 1).

I have dealt with list of values when using "in" clause.

example:

select * from table where id in(1,2,3,4) etc

here 1,2,3,4 is nothing but a array

I want to update the colorcode column on similar lines but I don't know how to access the elements of my array based in index.

after running the update statement my expected output is

id   name               location     colorcode
121  Royal Challengers  Bangalore     #FF8484
122  Sun Risers         Hyderabad     #FF82A9
123  Dare Devils        Delhi         #FA82FF
124  Gujrat Lions       Ahmedabad     #C682FF

I can sort the result based on id ie., the primary key

Note: I am using Postgres

trincot

You could do it as follows. I assume your table is called t:

update t
set    colorcode = middle.cc
from   (
        select id, ('{#FF8484,#FF82A9,#FA82FF,#C682FF,#8782FF}'::text[])[rn] as cc
        from   (select id, row_number() over (order by id) as rn from t) as base
        where  rn <= 5
       ) as middle
where middle.id = t.id;

In the most inner query (base), the row number is retrieved for each record in the table. Then in the middle query (middle) that row number is used to fetch the corresponding colour code from an in-line array of text elements. Finally the update statement joins that result with the table t again, by its id, in order to store that colour code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update column of a table based on array of varchar in PostgreSQL

From Dev

update column of a table in PostgreSQL

From Dev

Update a column in a table based on column in another table

From Dev

Update a column based on other column with postgresql

From Dev

Update table column based on columns in other table

From Dev

How to update table based on another table column?

From Dev

Update SQL Table based on column values

From Dev

Update table column values based on conditional logic

From Dev

update existing table column based on condition

From Dev

SQL Update Column based on separate table conditions

From Dev

PostgreSql: how to update table from array?

From Dev

Setting a JSON array of strings as a column in a PostgreSQL table

From Dev

PostgreSQL: Extract column into separate table and update other column with related key

From Dev

PostgreSQL update column from another column in same table

From Java

How to update column based on another column in the same table

From Dev

Mysql - update table column from another column based on order

From Dev

Update value in a column based on another column in the same table in MYSQL

From Dev

Update value in column based on column count in another table with mysql

From Dev

Update 3 column in same time 2nd table all field varchar update help of phone number

From Dev

Create vertical column table based on Array of Objects

From Dev

R update table column based on search string from another table

From Dev

Update column in one table based on value in another table in mysql

From Dev

R update table column based on search string from another table

From Dev

How to update a column in one table based on values from a second table?

From Dev

Update column in one table based on value in another table in mysql

From Dev

Update table based on values of column in other 2 table

From Dev

update column in one table based on condition in another table

From Dev

SQLite - Update a column based on values from another table's columns

From Dev

Update statement to set a column based the maximum row of another table

Related Related

  1. 1

    Update column of a table based on array of varchar in PostgreSQL

  2. 2

    update column of a table in PostgreSQL

  3. 3

    Update a column in a table based on column in another table

  4. 4

    Update a column based on other column with postgresql

  5. 5

    Update table column based on columns in other table

  6. 6

    How to update table based on another table column?

  7. 7

    Update SQL Table based on column values

  8. 8

    Update table column values based on conditional logic

  9. 9

    update existing table column based on condition

  10. 10

    SQL Update Column based on separate table conditions

  11. 11

    PostgreSql: how to update table from array?

  12. 12

    Setting a JSON array of strings as a column in a PostgreSQL table

  13. 13

    PostgreSQL: Extract column into separate table and update other column with related key

  14. 14

    PostgreSQL update column from another column in same table

  15. 15

    How to update column based on another column in the same table

  16. 16

    Mysql - update table column from another column based on order

  17. 17

    Update value in a column based on another column in the same table in MYSQL

  18. 18

    Update value in column based on column count in another table with mysql

  19. 19

    Update 3 column in same time 2nd table all field varchar update help of phone number

  20. 20

    Create vertical column table based on Array of Objects

  21. 21

    R update table column based on search string from another table

  22. 22

    Update column in one table based on value in another table in mysql

  23. 23

    R update table column based on search string from another table

  24. 24

    How to update a column in one table based on values from a second table?

  25. 25

    Update column in one table based on value in another table in mysql

  26. 26

    Update table based on values of column in other 2 table

  27. 27

    update column in one table based on condition in another table

  28. 28

    SQLite - Update a column based on values from another table's columns

  29. 29

    Update statement to set a column based the maximum row of another table

HotTag

Archive