用空值替换零 - 遇到错误

问题描述 投票:0回答:2

我正在 BigQuery 中处理天气数据,在分析风速和能见度之前,需要将零(错误输入的缺失值)替换为空值。我当前的代码导致错误。

任何人都可以帮助我使用正确的语法来实现这个空值替换吗?分享相关的代码片段和错误消息将不胜感激。

sql google-bigquery data-cleaning
2个回答
0
投票

以下应该有效:

UPDATE tablename
SET columnname = NULL
WHERE columnname = 0

我能想到的唯一潜在错误是该列是否被声明为

NOT NULL
。您需要首先使用
ALTER TABLE
启用列中的空值。


0
投票

在 BigQuery 中,您可以使用 NULLIF() 函数将零替换为 null 值。以下是直接更新现有表的方法:

UPDATE your_table
SET wind_speed = NULLIF(wind_speed, 0), #adjust the column name wind_speed
    visibility = NULLIF(visibility, 0)  #adjust the column name visibility
WHERE wind_speed = 0 OR visibility = 0;

您可以使用类似的方法来创建表的视图,而不是更新现有表:

SELECT
  desired_columns, #replace with all the desired column names.
  NULLIF(wind_speed, 0) AS wind_speed,
  NULLIF(visibility, 0) AS visibility,
  -- include other columns here
FROM
  your_table_name
© www.soinside.com 2019 - 2024. All rights reserved.