This is probably a pretty simple question, but I have two separate queries that I would like to join into one table. One of the queries shows sign ups to my website by month and the other query shows sign ups that have made a purchase. I would like to combine the two queries into one table so I have the following table:
Month Sign-ups Sign-ups with Purchase Jan 2019 250 40 Feb 2019 500 120
How can I do that? Here’s my two queries:
Sign ups:
select count(u.id) as Sign_Ups , month(From_iso8601_timestamp(u.created)) as Month from prodjoinreel.users u where year(From_iso8601_timestamp(u.created)) = 2019 group by 2 order by 2 asc
Sign ups with purchase:
select count(distinct g.owner) as Sign_Ups_with_Reel , month(From_iso8601_timestamp(u.created)) as Month from prodjoinreel.goals g right join prodjoinreel.users u on g.owner = u.id where year(From_iso8601_timestamp(u.created)) = 2019 group by 2 order by 2 asc
thank you!
Advertisement
Answer
I think that you can just add a count(distinct u.id)
to the second query:
select month(From_iso8601_timestamp(u.created)) as Month, count(distinct u.id) as Sign_Ups, count(distinct g.owner) as Sign_Ups_with_Reel from prodjoinreel.users u left join prodjoinreel.goals g on g.owner = u.id where year(From_iso8601_timestamp(u.created)) = 2019 group by 1 order by 1
Note: I changed your right join
to a left join
; most people tend to consider that right join
s are rather counter-intuitive, and favor left join
s instead.