Select 未购买产品 X 的客户 ID
Select Customer ID who hasnt purchased product X
我有 table 个客户 ID 和购买的产品。随着时间的推移,一个客户 ID 可以购买多个产品。
客户ID、产品ID
在 BigQuery 中,我需要为那些没有购买产品 A 的人找到 CustomerID。
我一直在兜圈子试图做自连接,内连接,但我一无所知。
感谢任何帮助。
select customerID
from your_table
group by customerID
having sum(case when productID = 'A' then 1 else 0 end) = 0
并检查它是否只包含一个名字
sum(case when productID contains 'XYZ' then 1 else 0 end) = 0
如果您有客户 table,您可能需要:
select c.*
from customers c
where not exists (select 1 from t where t.customer_id = c.customer_id and t.proectID = 'A');
这将 return 没有购买的客户以及购买了除产品 A 以外的所有产品的客户。当然,您的数据中客户的定义可能是客户进行了购买,在这种情况下,我喜欢 Juergen 的解决方案。
以下适用于 BigQuery 标准 SQL
#standardSQL
SELECT CustomerID
FROM `project.dataset.yourTable`
GROUP BY CustomerID
HAVING COUNTIF(Product = 'A') = 0
您可以使用下面的虚拟数据测试/玩它
#standardSQL
WITH `project.dataset.yourTable` AS (
SELECT 1234 CustomerID, 'A' Product UNION ALL
SELECT 11234, 'A' UNION ALL
SELECT 4567, 'A' UNION ALL
SELECT 7896, 'C' UNION ALL
SELECT 5432, 'B'
)
SELECT CustomerID
FROM `project.dataset.yourTable`
GROUP BY CustomerID
HAVING COUNTIF(Product = 'A') = 0
how would I adjust this so it could be productID contains "xyz"
#standardSQL
WITH `project.dataset.yourTable` AS (
SELECT 1234 CustomerID, 'Axyz' Product UNION ALL
SELECT 11234, 'A' UNION ALL
SELECT 4567, 'A' UNION ALL
SELECT 7896, 'Cxyz' UNION ALL
SELECT 5432, 'B'
)
SELECT CustomerID
FROM `project.dataset.yourTable`
GROUP BY CustomerID
HAVING COUNTIF(REGEXP_CONTAINS(Product, 'xyz')) = 0
我有 table 个客户 ID 和购买的产品。随着时间的推移,一个客户 ID 可以购买多个产品。
客户ID、产品ID
在 BigQuery 中,我需要为那些没有购买产品 A 的人找到 CustomerID。
我一直在兜圈子试图做自连接,内连接,但我一无所知。
感谢任何帮助。
select customerID
from your_table
group by customerID
having sum(case when productID = 'A' then 1 else 0 end) = 0
并检查它是否只包含一个名字
sum(case when productID contains 'XYZ' then 1 else 0 end) = 0
如果您有客户 table,您可能需要:
select c.*
from customers c
where not exists (select 1 from t where t.customer_id = c.customer_id and t.proectID = 'A');
这将 return 没有购买的客户以及购买了除产品 A 以外的所有产品的客户。当然,您的数据中客户的定义可能是客户进行了购买,在这种情况下,我喜欢 Juergen 的解决方案。
以下适用于 BigQuery 标准 SQL
#standardSQL
SELECT CustomerID
FROM `project.dataset.yourTable`
GROUP BY CustomerID
HAVING COUNTIF(Product = 'A') = 0
您可以使用下面的虚拟数据测试/玩它
#standardSQL
WITH `project.dataset.yourTable` AS (
SELECT 1234 CustomerID, 'A' Product UNION ALL
SELECT 11234, 'A' UNION ALL
SELECT 4567, 'A' UNION ALL
SELECT 7896, 'C' UNION ALL
SELECT 5432, 'B'
)
SELECT CustomerID
FROM `project.dataset.yourTable`
GROUP BY CustomerID
HAVING COUNTIF(Product = 'A') = 0
how would I adjust this so it could be productID contains "xyz"
#standardSQL
WITH `project.dataset.yourTable` AS (
SELECT 1234 CustomerID, 'Axyz' Product UNION ALL
SELECT 11234, 'A' UNION ALL
SELECT 4567, 'A' UNION ALL
SELECT 7896, 'Cxyz' UNION ALL
SELECT 5432, 'B'
)
SELECT CustomerID
FROM `project.dataset.yourTable`
GROUP BY CustomerID
HAVING COUNTIF(REGEXP_CONTAINS(Product, 'xyz')) = 0