3

I have an events table with 3 columns:

  • sequence_id (the "offer" shown to many different cart sessions)
  • checkout_token (the cart session for a single visitor)
  • action (the type of event generated by the user with that cart session token)

I want to output data in the following (NOT real numbers according to my image, just dummy example of output format):

sequence_id      action_0_unique_count    action_1_unique_count
1                5                        4
2                2                        0

im trying to count the DISTINCT number of event types (action enum column) PER checkout session, grouped by sequence_id

enter image description here

Here's an image example of which rows I'm trying to select for column 2. Column 3 would be similar, except WHERE action = 1

1 Answer 1

5

You can use conditional aggregation:

select sequence_id,
       count(distinct checkout_token) filter (where action = 0),
       count(distinct checkout_token) filter (where action = 1)
from events
group by sequence_id;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much, always there is a simple answer that just... eludes me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.