简体   繁体   English

如何使用 SQL 在表中创建一个新列以仅显示来自另一列的特定数据?

[英]How can I create a new column in a table to show only specific data from another column using SQL?

I am trying to figure out how to create a new column in a table that will be labelled as Release_Year and that will only show the year that each movie was released.我想弄清楚如何在表中创建一个新列,该列将被标记为 Release_Year,并且只会显示每部电影的发行年份。

Example例子

I have not attempted to run a query as I do not know where to begin.我没有尝试运行查询,因为我不知道从哪里开始。

You need to add a new column of type int.您需要添加一个新的 int 类型的列。

ALTER TABLE `projectID.datasetID.movie_table`
ADD COLUMN Release_Year int64;

Then you need to fill the column with values, you can do this using a merge statement.然后您需要用值填充该列,您可以使用merge语句来完成此操作。 We also use a group by to ensure we have unique mappings.我们还使用 group by 来确保我们拥有唯一的映射。

merge into `projectID.datasetID.movie_table` as target_table
using (
select
movie_title,
extract(year from release_date) as release_year
from
`projectID.datasetID.movie_table`
group by
movie_title,
release_year) as mapping
on
mapping.movie_title = target_table.movie_title
when matched
then
update set target_table.release_year = mapping.release_year

This is untested but the general idea should work.这是未经测试的,但总体思路应该可行。

暂无
暂无

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

相关问题 如何将一列的数据加载到同一个表的另一列中 - How can I load data of one column into another column in the same table 使用 `string_agg` 在 BigQuery SQL 中创建一个包含另一个连接的唯一元素的新列 - Create a new column containing the concatenated unique elements of another in BigQuery SQL, using `string_agg` SQL redshift:用另一个表的数据更新列但出现错误,为什么? - SQL redshift: Updating a column with data from another table but get an error, why? 创建新的 sql 列以获得新的价值 - create new sql column to get new value 创建具有 4 个值的新列并以 4 种方式从表中拆分现有值 - Create new column with 4 values and split existing value from table 4 ways 如何将数据从一个表复制到另一个表中,该表在 GCP Bigquery 中有一个记录重复列 - How to copy data from one table into another table which has a record repeated column in GCP Bigquery 如何只为特定的一个分配新的列值 - How assing new column value for only specific one 如何通过 Python 脚本向 Athena exists 表中的特定 position 添加一列? - How can I add a column to specific position in Athena exists table by Python script? 在 Google BigQuery SQL 中,当左连接到另一个表时,如何找到特定列的最小值? - How do you pull find the min value of a specific column, when left joined to another table, in Google BigQuery SQL? 如何从按特定列分区的表中计算滚动时间戳总和? - SQL - How to calculate rolling timestamp sum from table partitioned by specific column? - SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM