Google bigquery 将一列中的值替换为同一行中具有特定条件BQ的另一列中的值

Google bigquery 将一列中的值替换为同一行中具有特定条件BQ的另一列中的值,google-bigquery,Google Bigquery,如果条件满足,我想用另一列中的值替换一列中的值。 数据如下所示: FieldName Previous AA 1 null 1 null 0 BB 1 BB 1 我想用同一行中FieldName中的值替换上一列中的值1,但仅当FieldName不为null时 输出: FieldName Previous AA AA null 1 null 0 BB BB BB

如果条件满足,我想用另一列中的值替换一列中的值。 数据如下所示:

FieldName Previous 
AA         1
null       1
null       0
BB         1
BB         1
我想用同一行中FieldName中的值替换上一列中的值1,但仅当FieldName不为null时

输出:

FieldName Previous 
AA         AA
null       1
null       0
BB         BB
BB         BB

我已经阅读了有关强制转换和替换的文档,但无法将其应用到我的案例中。

下面是针对BigQuery标准SQL的

#standardSQL
SELECT FieldName, 
  IF(FieldName IS NULL, Previous, FieldName) AS Previous
FROM `project.dataset.table`   
您可以使用问题中的示例数据来处理上述问题,如下例所示

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 'AA' FieldName, '1' Previous UNION ALL
  SELECT NULL, '1' UNION ALL
  SELECT NULL, '0' UNION ALL
  SELECT 'BB', '1' UNION ALL
  SELECT 'BB', '1' 
)
SELECT FieldName, 
  IF(FieldName IS NULL, Previous, FieldName) AS Previous
FROM `project.dataset.table`  
有输出

Row FieldName   Previous     
1   AA          AA   
2   null        1    
3   null        0    
4   BB          BB   
5   BB          BB   

您可以在同一个表中使用update命令来实现这一点

CREATE TABLE `testdataset.t1` (
  `FieldName` STRING,
  `Previous` STRING,
);

insert into `testdataset.t1`
values ('AA','1'), (null,'1'),(null,'0'),('BB','1'),('BB','1');

UPDATE `testdataset.t1`
SET Previous = FieldName
where FieldName is not null;