左外连接或 where 语句?
Left outer join or where statement?
我有一个查询可以提供同一日期四个不同 table 中的记录总数。我将它们过滤为五个相关项目并排除一种类型。
SELECT
COUNT (*) AS table_total_filtered
FROM
db.table_1_20150727,
db.table_2_20150727,
db.table_3_20150727,
db.table_4_20150727
WHERE
item LIKE "A" OR item LIKE "B" OR item LIKE "C"
OR item LIKE "D" OR item LIKE "E" AND
type NOT LIKE "Z"
我有第二个查询,它给出了我第二个 table 中的行数。数据中可能存在时区差异,因此我将 table 拉了三天,我的目标日期是中间日期。
SELECT
COUNT (*) AS table_2_total
FROM
db2.table_20150726,
db2.table__20150727,
db2.table__20150728
两个table有共同的领域。公共字段在每个 table 中都有不同的名称。该字段在 table_1、table_2、table_3 和 table_4 中称为 ID1。它在table中被称为ID2。我想获取第二个查询的结果,然后计算 ID 在两个查询中匹配的次数。
SELECT
COUNT (*) AS overlap
FROM
db.table_1_20150727,
db.table_2_20150727,
db.table_3_20150727,
db.table_4_20150727,
db2.table_20150726,
db2.table__20150727,
db2.table__20150728
WHERE
item LIKE "A" OR item LIKE "B" OR item LIKE "C"
OR item LIKE "D" OR item LIKE "E" AND
type NOT LIKE "Z" AND
ID1 = ID2
据我了解,此查询不起作用,因为第一步采用第一个 table 的计数,并试图计算不存在的 ID2。使用 table 名称限定字段似乎也不起作用,因为文本不会变成蓝色。我相信目前排位赛不是我的问题,我需要做的是将第一个查询的结果与第二个查询的结果进行左连接,然后计算 ID1=ID2?
非常感谢任何有关如何 write/approach 适当的语句来连接这两个查询并计算 ID1 与 ID2 匹配的记录数的帮助!
谢谢!
听起来您想要一个非常简单的 JOIN
,每个初始查询都作为来源:
SELECT
COUNT(*) as overlap
FROM (
SELECT
ID1
FROM
db.table_1_20150727,
db.table_2_20150727,
db.table_3_20150727,
db.table_4_20150727
WHERE
item LIKE "A" OR item LIKE "B" OR item LIKE "C"
OR item LIKE "D" OR item LIKE "E" AND
type NOT LIKE "Z") AS table_1
JOIN (
SELECT
ID2
FROM
db2.table_20150726,
db2.table__20150727,
db2.table__20150728) as table_2
ON table_1.ID1 = table_2.ID2
请注意,在许多 SQL 系统中,逗号表示 JOIN
,但 in BigQuery, the comma actually represents a union:
Note: Unlike many other SQL-based systems, BigQuery uses the comma syntax to indicate table unions, not joins.
这可能就是您在上面发布的查询未达到您预期的结果的原因。
我有一个查询可以提供同一日期四个不同 table 中的记录总数。我将它们过滤为五个相关项目并排除一种类型。
SELECT
COUNT (*) AS table_total_filtered
FROM
db.table_1_20150727,
db.table_2_20150727,
db.table_3_20150727,
db.table_4_20150727
WHERE
item LIKE "A" OR item LIKE "B" OR item LIKE "C"
OR item LIKE "D" OR item LIKE "E" AND
type NOT LIKE "Z"
我有第二个查询,它给出了我第二个 table 中的行数。数据中可能存在时区差异,因此我将 table 拉了三天,我的目标日期是中间日期。
SELECT
COUNT (*) AS table_2_total
FROM
db2.table_20150726,
db2.table__20150727,
db2.table__20150728
两个table有共同的领域。公共字段在每个 table 中都有不同的名称。该字段在 table_1、table_2、table_3 和 table_4 中称为 ID1。它在table中被称为ID2。我想获取第二个查询的结果,然后计算 ID 在两个查询中匹配的次数。
SELECT
COUNT (*) AS overlap
FROM
db.table_1_20150727,
db.table_2_20150727,
db.table_3_20150727,
db.table_4_20150727,
db2.table_20150726,
db2.table__20150727,
db2.table__20150728
WHERE
item LIKE "A" OR item LIKE "B" OR item LIKE "C"
OR item LIKE "D" OR item LIKE "E" AND
type NOT LIKE "Z" AND
ID1 = ID2
据我了解,此查询不起作用,因为第一步采用第一个 table 的计数,并试图计算不存在的 ID2。使用 table 名称限定字段似乎也不起作用,因为文本不会变成蓝色。我相信目前排位赛不是我的问题,我需要做的是将第一个查询的结果与第二个查询的结果进行左连接,然后计算 ID1=ID2?
非常感谢任何有关如何 write/approach 适当的语句来连接这两个查询并计算 ID1 与 ID2 匹配的记录数的帮助!
谢谢!
听起来您想要一个非常简单的 JOIN
,每个初始查询都作为来源:
SELECT
COUNT(*) as overlap
FROM (
SELECT
ID1
FROM
db.table_1_20150727,
db.table_2_20150727,
db.table_3_20150727,
db.table_4_20150727
WHERE
item LIKE "A" OR item LIKE "B" OR item LIKE "C"
OR item LIKE "D" OR item LIKE "E" AND
type NOT LIKE "Z") AS table_1
JOIN (
SELECT
ID2
FROM
db2.table_20150726,
db2.table__20150727,
db2.table__20150728) as table_2
ON table_1.ID1 = table_2.ID2
请注意,在许多 SQL 系统中,逗号表示 JOIN
,但 in BigQuery, the comma actually represents a union:
Note: Unlike many other SQL-based systems, BigQuery uses the comma syntax to indicate table unions, not joins.
这可能就是您在上面发布的查询未达到您预期的结果的原因。