我在BQ中有一个TableA,其中包含customer_id和推荐产品(ean)列的列表。我有一个View B,其中包含所有销售数据,包括customer_ids和ean。
我想创建一个表或写一个查询,我会过滤掉客户已经购买的所有推荐的eans。
这是我到目前为止所做的:
SELECT
backend_customer_id,
ean
FROM
TableA
WHERE
1=1
AND backend_customer_id IN (
SELECT
backend_customer_id
FROM
ViewB
WHERE
ean NOT IN (
SELECT
ean
FROM
TableA))
ORDER BY
1,2
出于某种原因,并非所有购买的ean都被过滤掉了。 我正在使用旧的SQL语法。
答案 0 :(得分:1)
也许标准SQL中的此查询可让您深入了解如何在Legacy中调整查询:
WITH table_A AS(
select '1' as customer, '1' recommended_ean union all
select '1' as customer, '2' recommended_ean union all
select '1' as customer, '3' recommended_ean union all
select '2' as customer, '1' recommended_ean
),
table_b as(
select '1' as customer, '1' purchased_ean union all
select '2' as customer, '2' purchased_ean
)
SELECT
customer,
recommended_ean
FROM
table_A a
WHERE 1 = 1
AND NOT EXISTS(SELECT 1 FROM table_B b WHERE a.customer = b.customer AND a.recommended_ean = b.purchased_ean)
ORDER BY 1, 2
请注意,在您的查询中,您实际上并未过滤掉EAN,而是过滤客户。如果客户没有购买任何东西,那么继续返回所有推荐的eans。
您应该调整查询以使用WHERE
子句过滤EAN,例如:
WHERE 1 = 1
AND recommended_ean NOT IN (SELECT purchased_ean FROM table_b b WHERE a.customer = b.customer)
我强烈建议您开始使用BigQuery的Standard Version,因为它提供了更多功能,并且整体语法有所改进。
答案 1 :(得分:1)
我认为您需要的查询更简单一点:
SELECT backend_customer_id, ean
FROM TableA a LEFT JOIN
Viewb b
ON a.backend_customer_id = b.backend_customer_id AND
a.ean = b.ean
WHERE b.backend_customer_id IS NULL;
即,做一个LEFT JOIN
。然后保留所有不匹配的记录。
答案 2 :(得分:1)
以下是BigQuery Standard SQL
#standardSQL
SELECT t.backend_customer_id, t.ean
FROM TableA t LEFT JOIN ViewB v
ON (t.backend_customer_id, t.ean) = (v.backend_customer_id, v.ean)
WHERE v.ean IS NULL