如何从 Snowflake 中的另一个 table select table 中的所有内容?

How to select everything NOT in a table from another table in Snowflake?

所以我有两个 tables :

Table 1.

ID  value value2
001     A    blue 
002     V     red
003     A    blue
004     V   green
005     X    blue

Table 2.

ID  value value2
001     A    blue 
004     V   green

我想做的就是使用 CTE(考虑到数据的性质,这很重要),return table2 ID 没有出现在 [=40= 中的实例] 1. 看起来像:

CTE 结果

ID  value  value2
002     V     red
003     A    blue
005     X    blue

所以我期待的是:

WITH t1 AS (
SELECT ID, value,value2
FROM table1
),
t2 AS (
SELECT ID, value,value2
FROM table2
)
SELECT 
??????

我认为这将是某种连接,但我正在努力以这种方式正确地做到这一点。

MINUS 方式:

好吧根本不是 CTE,但鉴于您的行“完全相同”,您可以做 MINUS:

SELECT id, value, value2 FROM table1
MINUS 
SELECT id, value, value2 FROM table2;
ID VALUE VALUE2
002 V red
003 A blue
005 X blue

在使用 CTE 的情况下 prefetch/condition 数据可以是:

WITH t1 AS (
  SELECT ID, value,value2
  FROM table1
), t2 AS (
  SELECT ID, value,value2
  FROM table2
)
SELECT id, value, value2 FROM table1
MINUS 
SELECT id, value, value2 FROM table2

现在,如果您的 CTE 将所有列对齐,则可以简化为:

WITH t1 AS (
  SELECT ID, value,value2
  FROM table1
), t2 AS (
  SELECT ID, value,value2
  FROM table2
)
SELECT * FROM table1
MINUS 
SELECT * FROM table2

LEFT JOIN 方式:

这也可以通过添加 WHERE IS NULL 子句来使用 LEFT JOIN(称为 LEFT OUTER JOIN)来完成:

WITH t1 AS (
  SELECT ID, value,value2
  FROM table1
), t2 AS (
  SELECT ID, value,value2
  FROM table2
)
SELECT t1.id, t1.value, t1.value2 
FROM table1 as t1
LEFT JOIN table2 as t2
    ON t1.id = t2.id /* plus other clauses if you want those as well */
WHERE t2.id IS NULL
ID VALUE VALUE2
002 V red
003 A blue
005 X blue

您所求的文字:

return the instances where the table2 ID's do not appear in table 1.

表示为 LEFT JOIN:

WITH t1 AS (
  SELECT ID, value,value2
  FROM table1
), t2 AS (
  SELECT ID, value,value2
  FROM table2
)
SELECT t2.id, t2.value, t2.value2 
FROM table2 as t2
LEFT JOIN table1 as t1
    ON t1.id = t2.id /* plus other clauses if you want those as well */
WHERE t1.id IS NULL

没有给出结果:

因为 001 & 004 都在 table1

这也可以表示为 RIGHT JOIN:

WITH t1 AS (
  SELECT ID, value,value2
  FROM table1
), t2 AS (
  SELECT ID, value,value2
  FROM table2
)
SELECT t2.id, t2.value, t2.value2 
FROM table1 as t1
RIGHT JOIN table2 as t2
    ON t1.id = t2.id /* plus other clauses if you want those as well */
WHERE t1.id IS NULL

仍然returns没有结果。

CTE 对我来说很有趣

我几乎总是使用 CTE 为人们的问题输入“虚假数据”,因此为了构建这个问题并“使用你的两个 CTE”,我不得不嵌套我的 CTE,这真的很有趣,但展示了如何嵌套它们,以及它们的强大程度:

WITH table1(ID, value, value2) as (
    SELECT * FROM VALUES
        ('001', 'A', 'blue'),
        ('002', 'V', 'red'),
        ('003', 'A', 'blue'),
        ('004', 'V', 'green'),
        ('005', 'X', 'blue')
), table2(ID, value, value2) as (
    SELECT * FROM VALUES
        ('001', 'A', 'blue'),
        ('004', 'V', 'green')
), cte as(
  
    
WITH t1 AS (
  SELECT ID, value,value2
  FROM table1
), t2 AS (
  SELECT ID, value,value2
  FROM table2
)
SELECT t2.id, t2.value, t2.value2 
FROM table1 as t1
RIGHT JOIN table2 as t2
    ON t1.id = t2.id /* plus other clauses if you want those as well */
WHERE t1.id IS NULL
    
    
)
SELECT * FROM cte;