0

i have a table like so:

email | segment | exit --------------------------------------------- [email protected] | abandoned_cart | true [email protected] | purchased_last_7_days | false [email protected] | abandoned_cart | true [email protected] | purchased_last_7_days | false [email protected] | abandoned_cart | true 

I am trying write a query to get a count of unique email addresses that have BOTH abandoned_cart = true and purchase_last_7_days = false

This is what I tried but recieved 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) 

1 Answer 1

1

Below is for BigQuery Standard 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 

You can test, play with above using sample data from your question as in below example

#standardSQL WITH `project.dataset.table` AS ( SELECT '[email protected]' email, 'abandoned_cart' segment, TRUE exit UNION ALL SELECT '[email protected]', 'purchased_last_7_days', FALSE UNION ALL SELECT '[email protected]', 'abandoned_cart', TRUE UNION ALL SELECT '[email protected]', 'purchased_last_7_days', FALSE UNION ALL SELECT '[email protected]', '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 

with result

Row email 1 [email protected] 2 [email protected] 

Note: above assumes there is no duplicate entries for segments/exit values for the same email (as it is looks like from your example)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.