2013-05-07 66 views
-1

我正在PostgreSQL數據庫上寫一個查詢,並且不知道如何在連接後執行非空值的計數。在MySQL中我會寫:postgresql vs mysql左連接

select count(a.clicker_id), count(b.clicker_id) from 
(select * from events where type='click') a 
left join (select * from tbl_events where type='conversion')b 
on a.clicker_id=b.clicker_id 

基本上,試圖寫沒有一個子查詢如下:

select date(cl.created_at), count(cl.click_id) as clicks, count(cp.click_id) as Conversions from events_table cl 
left outer join (select click_id, created_at from events_table where type='conversion_potential')cp 
on (cl.click_id=cp.click_id) 
where cl."type"='click' 
and cl.placement_id in (1,2,3) 
group by 1 
+5

由於你的'b.type = conversion'子句,你事實上正在做一個內部連接。仔細檢查你的MySQL查詢是否是你發佈的。 – 2013-05-07 12:18:48

+0

剛剛刪除我的答案,因爲是錯的。 – fog 2013-05-07 12:35:42

+0

在_RIGHT_查詢中是否真的有兩個表,事件和tbl_events? – 2013-05-07 13:05:36

回答

1

你並不需要一個自聯接:

select 
    count(type = 'click' or null) clicks, 
    count(type = 'conversion' or null) conversions 
from events_table 
where campaign_id = 555 
+0

我確實需要一個連接,因爲我在where子句中還有一些其他內容,例如來自廣告系列ID下的點擊。我只需要具有相同點擊ID的所選點擊次數的轉化。 轉化確實是一個類型值 – AdrianBR 2013-05-07 12:44:30

+0

@AdrianBR編輯。顯示您的完整需求,因爲我仍然認爲您不需要自我加入。 – 2013-05-07 12:52:06

+0

我解釋錯了,在表的第二個實例上有一些設置變量排除或包含發佈者。 – AdrianBR 2013-05-07 13:00:20

1

這難道不是工作:

select count(a.clicker_id), count(b.clicker_id) 
    from events_table as a 
     left join events_table as b 
     on a.clicker_id=b.clicker_id and b.type='conversion' 
where a.type='click' 
and campaign_id=555