从一个表到另一个表的行筛选

时间:2018-09-10 14:11:41

标签: google-bigquery

我有两个BigQuery表:

TABLE 1
+---------+---------+---------+
|  Col1   |  Col2   |  Col3   |
+---------+---------+---------+
| Value 1 | Value 2 | Value3  |
| Value 4 | Value 5 | Value 6 |
+---------+---------+---------+

TABLE 2
+---------+---------+--------+
|  Col1   |  Col2   |  Col4  |
+---------+---------+--------+
| Value 1 | Value 2 | Value7 |
+---------+---------+--------+

我需要从TABLE 1中删除Col1-Col2中存在的所有对TABLE 2

您可以使用以下方法构建测试数据

WITH

TABLE1 AS (
  SELECT "Value 1" AS Col1,"Value 2" as Col2, "Value3" AS Col3
  UNION ALL
  SELECT "Value 4","Value 5", "Value 6"
),

TABLE2 AS (
  SELECT "Value 1" AS Col1,"Value 2" as Col2, "Value7" AS Col4
)

我尝试了这种表示法,但是它不起作用

SELECT * FROM TABLE1
EXCEPT DISTINCT (
  SELECT Col1, Col2 FROM TABLE2
)

我发现的唯一方法是首先过滤线并将它们连接在一起:

SELECT t1.*, t2.* EXCEPT(Col1,Col2)

FROM (
  SELECT Col1,Col2
  FROM TABLE1
  EXCEPT DISTINCT (
    SELECT Col1, Col2 FROM TABLE2
)) AS t1
LEFT JOIN TABLE1 AS t2 ON t1.Col1 = t2.Col1 AND t1.Col2 = t2.Col2

您知道仅使用EXCEPT函数的简单方法吗?

输出是根据第二个表的前2列过滤的第一个表中的行

+---------+---------+---------+
|  Col1   |  Col2   |  Col3   |
+---------+---------+---------+
| Value 4 | Value 5 | Value 6 |
+---------+---------+---------+

1 个答案:

答案 0 :(得分:2)

#standardSQL
WITH TABLE1 AS (
  SELECT "Value 1" AS Col1,"Value 2" AS Col2, "Value3" AS Col3 UNION ALL
  SELECT "Value 4","Value 5", "Value 6"
), TABLE2 AS (
  SELECT "Value 1" AS Col1,"Value 2" AS Col2, "Value7" AS Col4
)
SELECT a.*
FROM table1 a
LEFT JOIN (
  SELECT DISTINCT col1, col2
  FROM table2 
) b
USING(col1, col2)
WHERE b.col1 IS NULL   


Row Col1    Col2    Col3     
1   Value 4 Value 5 Value 6