gpt4 book ai didi

sql - bigquery中比较两个表的有效方法

转载 作者:行者123 更新时间:2023-12-01 16:43:34 28 4
gpt4 key购买 nike

我有兴趣比较两个表是否包含相同的数据。

我可以这样做:

#standardSQL
SELECT
key1, key2
FROM
(
SELECT
table1.key1,
table1.key2,
table1.column1 - table2.column1 as col1,
table1.col2 - table2.col2 as col2
FROM
`table1` AS table1
LEFT JOIN
`table2` AS table2
ON
table1.key1 = table2.key1
AND
table1.key2 = table2.key2
)
WHERE
col1 != 0
OR
col2 != 0

但是,当我想比较所有数字列时,这有点困难,尤其是当我想对多个表组合进行比较时。

因此,我的问题是:是否有人意识到有可能遍历所有数字列并将结果集限制为这些差异中的任何一个都不为零的那些键?

最佳答案

First, I want to bring up issues with your original query



主要问题是1)使用LEFT JOIN; 2)使用col!= 0

下面是应如何对其进行修改以真正捕获两个表中的所有差异
运行原始查询,然后运行以下查询-希望您会看到其中的区别
#standardSQL
SELECT key1, key2
FROM
(
SELECT
IFNULL(table1.key1, table2.key1) key1,
IFNULL(table1.key2, table2.key2) key2,
table1.column1 - table2.column1 AS col1,
table1.col2 - table2.col2 AS col2
FROM `table1` AS table1
FULL OUTER JOIN `table2` AS table2
ON table1.key1 = table2.key1
AND table1.key2 = table2.key2
)
WHERE IFNULL(col1, 1) != 0
OR IFNULL(col2, 1) != 0

或者您可以尝试针对虚拟数据运行原始版本和更高版本,以查看差异
#standardSQL
WITH `table1` AS (
SELECT 1 key1, 1 key2, 1 column1, 2 col2 UNION ALL
SELECT 2, 2, 3, 4 UNION ALL
SELECT 3, 3, 5, 6
), `table2` AS (
SELECT 1 key1, 1 key2, 1 column1, 29 col2 UNION ALL
SELECT 2, 2, 3, 4 UNION ALL
SELECT 4, 4, 7, 8
)
SELECT key1, key2
FROM
(
SELECT
IFNULL(table1.key1, table2.key1) key1,
IFNULL(table1.key2, table2.key2) key2,
table1.column1 - table2.column1 AS col1,
table1.col2 - table2.col2 AS col2
FROM `table1` AS table1
FULL OUTER JOIN `table2` AS table2
ON table1.key1 = table2.key1
AND table1.key2 = table2.key2
)
WHERE IFNULL(col1, 1) != 0
OR IFNULL(col2, 1) != 0

Secondly, below will highly simplify your overall query


#standardSQL
SELECT
IFNULL(table1.key1, table2.key1) key1,
IFNULL(table1.key2, table2.key2) key2
FROM `table1` AS table1
FULL OUTER JOIN `table2` AS table2
ON table1.key1 = table2.key1
AND table1.key2 = table2.key2
WHERE TO_JSON_STRING(table1) != TO_JSON_STRING(table2)

您可以使用与上面相同的虚拟数据示例进行测试
注意:在此解决方案中,您无需选择特定的列-只需比较所有列即可!但是如果您只需要比较特定的列-您仍然需要像下面的示例一样挑选它们
#standardSQL
SELECT
IFNULL(table1.key1, table2.key1) key1,
IFNULL(table1.key2, table2.key2) key2
FROM `table1` AS table1
FULL OUTER JOIN `table2` AS table2
ON table1.key1 = table2.key1
AND table1.key2 = table2.key2
WHERE TO_JSON_STRING((table1.column1, table1.col2)) != TO_JSON_STRING((table2.column1, table2.col2))

关于sql - bigquery中比较两个表的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51311774/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com