0

The task was - https://platform.stratascratch.com/coding/514-marketing-campaign-success-advanced/official-solution?code_type=1

And I'm a little bit confused in the code.

 SELECT user_id
 FROM marketing_campaign
 GROUP BY user_id
 HAVING count(DISTINCT created_at) > 1
    AND count(DISTINCT product_id) > 1)

I'm interested in HAVING, default code would be like 'select user_id, count(DISTINCT created_at), count(DISTINCT product_id) and something like this, but in this code its added to HAVING. Does this mean count(DISTINCT created_at) work in the ALL DATASET? BUT HOW IF ITS ALREADY GROUP BY background and then return only user_id which one like this condition?

4
  • For each user_id, check the HAVING clause! I.e. only return user_id's having more than 1 distinct created_at and also more than 1 distinct product_id. Commented Dec 21, 2023 at 9:58
  • HAVING claus will do this for the entire original data array, not for the aggregated? Commented Dec 21, 2023 at 10:02
  • Please read minimal reproducible example Commented Dec 21, 2023 at 11:28
  • HAVING works per grouping, so in this case per user_id Commented Dec 21, 2023 at 12:58

0

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.