簡體   English   中英

將具有不同 where 子句的兩個查詢連接到 1 個表中

[英]Joining two queries with different where clauses into 1 table

這可能是一個非常簡單的問題,但我想將兩個單獨的查詢合並到一個表中。 其中一個查詢顯示按月注冊我的網站,另一個查詢顯示已購買的注冊。 我想將兩個查詢合並到一個表中,所以我有下表:

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

謝謝你!

我認為您可以在第二個查詢中添加一個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 s 是相當違反直覺的,而傾向於left join s。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM