我有一个像这样的表:
email | segment | exit
---------------------------------------------
1@aol.com | abandoned_cart | true
1@aol.com | purchased_last_7_days | false
2@aol.com | abandoned_cart | true
2@aol.com | purchased_last_7_days | false
3@aol.com | abandoned_cart | true
我正在尝试编写查询以获取具有两个abandoned_cart
= true
和purchase_last_7_days
= false
这是我尝试过但收到的0:
SELECT COUNT(DISTINCT email)
FROM `table_seg_changes`
WHERE (segment_slug = 'purchased_last_7_days' AND exit = false) AND (segment_slug = 'abandoned_cart' AND exit = true)
答案 0 :(得分:1)
以下是用于BigQuery标准SQL
#standardSQL
SELECT email
FROM `project.dataset.table`
GROUP BY email
HAVING COUNTIF(
(segment = 'abandoned_cart' AND exit) OR
(segment = 'purchased_last_7_days' AND NOT exit)
) = 2
您可以使用问题中的示例数据来进行测试,如上示例所示
#standardSQL
WITH `project.dataset.table` AS (
SELECT '1@aol.com' email, 'abandoned_cart' segment, TRUE exit UNION ALL
SELECT '1@aol.com', 'purchased_last_7_days', FALSE UNION ALL
SELECT '2@aol.com', 'abandoned_cart', TRUE UNION ALL
SELECT '2@aol.com', 'purchased_last_7_days', FALSE UNION ALL
SELECT '3@aol.com', 'abandoned_cart', TRUE
)
SELECT email
FROM `project.dataset.table`
GROUP BY email
HAVING COUNTIF(
(segment = 'abandoned_cart' AND exit) OR
(segment = 'purchased_last_7_days' AND NOT exit)
) = 2
有结果
Row email
1 1@aol.com
2 2@aol.com
注意:上面假设同一电子邮件的段/退出值没有重复的条目(如您的示例所示)