这可能是一个非常简单的问题,但是我想将两个独立的查询加入一个表中。其中一个查询显示按月注册我的网站,另一个查询显示已购买的注册。我想将两个查询合并到一个表中,所以有下表:
Month Sign-ups Sign-ups with Purchase
Jan 2019 250 40
Feb 2019 500 120
我该怎么做?这是我的两个查询:
注册:
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
注册购买:
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
谢谢!
答案 0 :(得分:0)
我认为您可以在第二个查询中添加一个count(distinct u.id)
:
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
注意:我将您的right join
更改为left join
;大多数人倾向于认为right join
是违反直觉的,而倾向于left join
。