Skip to content
Advertisement

Subqueries on same table

What I want to do is run a subquery from the same table in a single query my SQL is below:

Straight forward and this gets me EXACTLY what I want – number of clicks on each day of a given month in a given year.

From THIS result, I’m looking to find how many of these clicks fell into a particular category, i.e. how many are to social media, how many are to email, etc. for EACH day (daily total, then breakdown of that total). I can get that fairly easily by adding another AND operator to the SQL,

but that involves creating another full statement; I want to have these results from one SINGLE query.

Looping the results in various programming languages gives me the desired result, but it’s not the most efficient means. I’ve tried unions and various joins, but I’m not finding the right spot to run the subquery.

Advertisement

Answer

Just use conditional aggregation:

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