简体   繁体   English

根据 redshift 中另一列的值创建列

[英]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:我想根据id列的 map 创建另一列,例如:

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:所以我基本上想要一个查询来创建一个基于 map 的列,我的最终表是:

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

I have this map in a python dictionary.我在 python 字典中有这个 map。

Is this possible?这可能吗?

(It is basically pandas.map ) (基本上是pandas.map

It appears that you wish to "fix" some data that is already in your PostgreSQL database.您似乎希望“修复”一些已经存在于您的 PostgreSQL 数据库中的数据。

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.您可以将其作为UPDATE语句来执行,但它会变得棘手。 It would probably be easier to craft a SELECT statement that has everything you want, then use CREATE TABLE new_table AS SELECT...制作一个包含你想要的一切的SELECT语句可能会更容易,然后使用CREATE TABLE new_table AS SELECT...

See: CREATE TABLE AS - Amazon Redshift请参阅: 创建表为 - Amazon Redshift

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM