mysql 将子查询排除在连接之外
mysql keep subquery out of a join
我想计算 pid_raw 总数中 (pid = 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
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)
我的 sudo 代码:
select count(a.pid)/(select count(b.pid_raw) from b) AS pid_percentage, a.date from
table1 a join table2 b
ON a.pid = b.pid_raw
Where a.date - b.date_raw <=31 and a.date - b.date_raw > 0
group by a.date
order by YEAR(a.date),Month(a.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;
我想计算 pid_raw 总数中 (pid = 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
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)
我的 sudo 代码:
select count(a.pid)/(select count(b.pid_raw) from b) AS pid_percentage, a.date from
table1 a join table2 b
ON a.pid = b.pid_raw
Where a.date - b.date_raw <=31 and a.date - b.date_raw > 0
group by a.date
order by YEAR(a.date),Month(a.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;