BigQuery SQL 根据条件从目标 table 中删除行
BigQuery SQL delete rows from a target table based on a condition
在 BigQuery 中,我需要删除 table A 中的所有行(一个或多个)并从 table B 中插入一行,仅当 table B 中的值不为空时
Table答:
dateVal
2021-02-01
2021-02-10
Table乙:
dataValNew
2021-02-05
期望的输出:
Table答:
dateVal
2021-02-05
如果tableB是
dataValNew
null
期望的输出是(没有变化):
Table答:
dateVal
2021-02-01
2021-02-10
我尝试使用 Merge。类似于:
MERGE A as t
USING B as s
ON FALSE
WHEN NOT MATCHED BY SOURCE AND dataValNew is not null THEN
DELETE
WHEN NOT MATCHED AND dataValNew is not null THEN
INSERT ROW
但无法识别 dataValNew,因为它是来自源 table 的字段。
您可能不得不求助于脚本:
DECLARE cnt INT64 DEFAULT 0;
SET cnt = (SELECT COUNT(*) FROM B WHERE dataValNew is not NULL);
IF cnt > 0 THEN
CREATE OR REPLACE TABLE A AS SELECT dataValNew as dataVal FROM B;
END IF;
在 BigQuery 中,我需要删除 table A 中的所有行(一个或多个)并从 table B 中插入一行,仅当 table B 中的值不为空时
Table答:
dateVal
2021-02-01
2021-02-10
Table乙:
dataValNew
2021-02-05
期望的输出:
Table答:
dateVal
2021-02-05
如果tableB是
dataValNew
null
期望的输出是(没有变化):
Table答:
dateVal
2021-02-01
2021-02-10
我尝试使用 Merge。类似于:
MERGE A as t
USING B as s
ON FALSE
WHEN NOT MATCHED BY SOURCE AND dataValNew is not null THEN
DELETE
WHEN NOT MATCHED AND dataValNew is not null THEN
INSERT ROW
但无法识别 dataValNew,因为它是来自源 table 的字段。
您可能不得不求助于脚本:
DECLARE cnt INT64 DEFAULT 0;
SET cnt = (SELECT COUNT(*) FROM B WHERE dataValNew is not NULL);
IF cnt > 0 THEN
CREATE OR REPLACE TABLE A AS SELECT dataValNew as dataVal FROM B;
END IF;