Skip to content
Advertisement

Calculate the number of users based on the below conditions by writing a single query SQL

There are two campaigns running campaign A and Campaign B and list of user ids participated in those two campaign is given below.

Advertisement

Answer

Assuming you have two different tables, you can use union all and aggregation:

This gives you all the information you need. You can use group by in_a, in_b with rollup to get all combinations.

Or, you can summarize this into one row:

Note: These both assume that users are unique in each campaign. If not, just use select distinct or union in the subquery.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement