mysql将子查询排除在联接之外
我想计算(pid=pid_raw)占pid_raw总数的百分比,其中date_raw在日期前31天 我知道我可以用一个内部联接部分地完成这项工作,但因为我想得到百分比,因此需要pid_raw的总计数,而不管匹配与否,该子查询不能成为内部联接的一部分。如何编写子查询以获得不受内部联接影响但符合where子句的pid_raw的总计数mysql将子查询排除在联接之外,mysql,sql,join,subquery,Mysql,Sql,Join,Subquery,我想计算(pid=pid_raw)占pid_raw总数的百分比,其中date_raw在日期前31天 我知道我可以用一个内部联接部分地完成这项工作,但因为我想得到百分比,因此需要pid_raw的总计数,而不管匹配与否,该子查询不能成为内部联接的一部分。如何编写子查询以获得不受内部联接影响但符合where子句的pid_raw的总计数 table1 date pid 2015-06-01 223 2015-06-01 333 2015-05-01 124 201
table1
date pid
2015-06-01 223
2015-06-01 333
2015-05-01 124
2015-05-01 543
table2
date_raw pid_raw
2015-05-30 223
2015-05-15 111
2015-05-03 333
2015-05-02 242
2015-05-05 300
2015-04-10 124
2015-04-15 543
2015-04-09 511
Example output
date pid_percentage
2015-06-01 0.40 <-------(2/5)
2015-05-01 0.67 <------(2/3)
表1
日期pid
2015-06-01 223
2015-06-01 333
2015-05-01 124
2015-05-01 543
表2
日期\u原始pid\u原始
2015-05-30 223
2015-05-15 111
2015-05-03 333
2015-05-02 242
2015-05-05 300
2015-04-10 124
2015-04-15 543
2015-04-09 511
示例输出
日期pid_百分比
2015-06-01 0.40我的建议是在日期加入,然后使用条件聚合进行计算:
select t1.date,
count(distinct case when t1.pid = t2.pid_raw then t1.pid end) as NumMatches,
(count(distinct case when t1.pid = t2.pid_raw then t1.pid end) /
count(distinct case when t1.pid = t2.pid_raw then t2.pid_raw end)
) as percentage_pid
from table1 t1 left join
table2 t2
on t2.date_raw between t1.date - interval 31 day and t1.date
group by t1.date;
谢谢,虽然这会运行,但在我获得任何输出之前,它会花费太长时间和超时。有更好的方法吗?
select t1.date,
count(distinct case when t1.pid = t2.pid_raw then t1.pid end) as NumMatches,
(count(distinct case when t1.pid = t2.pid_raw then t1.pid end) /
count(distinct case when t1.pid = t2.pid_raw then t2.pid_raw end)
) as percentage_pid
from table1 t1 left join
table2 t2
on t2.date_raw between t1.date - interval 31 day and t1.date
group by t1.date;