简体   繁体   中英

Create column based on values on another column in redshift

Suppose I have the following table:

|---------------------|
|          id         |
|---------------------|
|          12         |
|---------------------|
|         390         |
|---------------------|
|          13         |
|---------------------|

And I want to create another column based on a map of the id column, for example:

12 -> qwert
13 -> asd
390 -> iop

So I basically want a query to create a column based on that map, my final table would be:

|---------------------|---------------------|
|          id         |          col        |
|---------------------|---------------------|
|          12         |          qwert      |
|---------------------|---------------------|
|         390         |          iop        |
|---------------------|---------------------|
|          13         |          asd        |
|---------------------|---------------------|

I have this map in a python dictionary.

Is this possible?

(It is basically pandas.map )

It appears that you wish to "fix" some data that is already in your PostgreSQL database.

You could include the data using this technique:

WITH foo AS (VALUES (12, 'qwert'), (13, 'asd'), (390, 'iop'))
SELECT table.id, foo.column2
FROM table
JOIN foo ON (foo.column1 = table.id)

You could do it as an UPDATE statement, but it gets tricky. It would probably be easier to craft a SELECT statement that has everything you want, then use CREATE TABLE new_table AS SELECT...

See: CREATE TABLE AS - Amazon Redshift

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-2025 STACKOOM.COM