【发布时间】:2016-11-07 21:46:31
【问题描述】:
我有一张这样的桌子:
userid | trackid | path
123 70000 ad
123 NULL abc.com
123 NULL Apply
345 70001 Apply
345 70001 Apply
345 NULL Direct
345 NULL abc.com
345 NULL cdf.com
我想要这样的查询。当path='abc.com'时,num_website +1;当 path='Apply', num_apply +1
userid | num_website | num_Apply | num_website/num_Apply
123 1 1 1
345 1 2 0.5
我的语法是这样的:
select * from
(select userid,count(path) as is_CWS
from TABLE
where path='abc.com'
group by userid
having count(path)>1) a1
JOIN
(select userid,count(userid) as Apply_num from
where trackid is not NULL
group by userid) a2
on a1.userid=a2.userid
我的问题是 1. 根据我上面的语法,如何拥有字段 num_website/num_apply? 2.有没有其他更简单的方法可以得到我想要的结果?
分享的任何景点都会感激不尽。
【问题讨论】:
-
什么是列别名“is_CWS”?还是 num_website ? where 子句“track_id is not null”是否适用于查询的顶部(别名为 a1)?看看我的查询有什么不同
-
您没有主键。这可能会在适当的时候证明是有问题的
标签: mysql postgresql join subquery