SQL 'GROUP BY' & 'Having' problem on stratascratch

51 views Asked by At

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?

0

There are 0 answers