我试图将两个表联合起来,比较一个匹配的字段,但我收到以下错误:-"SQL_ANALYSIS_ERROR: Unrecognized name: t2 (Line: 87, Column: 121) "其中t2被重命名为我的第二个表。
这是我的代码:-
CREATE OR REPLACE TABLE table_1 OPTIONS (
quota_accounting_owner='[email protected]')
AS
SELECT * FROM
(
SELECT * EXCEPT(fate_result), CAST(fate_result AS STRING) AS fate_result FROM table_1 AS t1 WHERE t2.e_id <> t1.e_id
UNION DISTINCT
SELECT * EXCEPT(fate_result), CAST(fate_result AS STRING) AS fate_result FROM table_2 AS t2
);
你可以使用 NOT EXISTS
的行,只选择 table_1
不匹配的 e_id
在 table_2
CREATE OR REPLACE TABLE table_1 OPTIONS (
quota_accounting_owner='[email protected]')
AS
SELECT * FROM
(
SELECT * EXCEPT(fate_result), CAST(fate_result AS STRING) AS fate_result
FROM table_1 AS t1
WHERE NOT EXISTS (
SELECT 1
FROM table_2 AS t2
WHERE t2.e_id = t1.e_id
)
UNION DISTINCT
SELECT * EXCEPT(fate_result), CAST(fate_result AS STRING) AS fate_result
FROM table_2 AS t2
);
编辑 NOT EXISTS对于标准SQL来说是正确的选择,但是看到下面的评论,这里会抛出一个错误,所以NOT IN可能更好。
CREATE OR REPLACE TABLE table_1 OPTIONS (
quota_accounting_owner='[email protected]')
AS
SELECT * FROM
(
SELECT * EXCEPT(fate_result), CAST(fate_result AS STRING) AS fate_result
FROM table_1 AS t1
WHERE t1.e_id NOT IN (
SELECT t2.ed_id
FROM table_2 AS t2
)
UNION DISTINCT
SELECT * EXCEPT(fate_result), CAST(fate_result AS STRING) AS fate_result
FROM table_2 AS t2
);