2013-05-07 78 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