【问题标题】:mysql keep subquery out of a joinmysql 将子查询排除在连接之外
【发布时间】:2023-04-09 02:53:02
【问题描述】:

我想计算 (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 
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);

【问题讨论】:

    标签: mysql sql join subquery


    【解决方案1】:

    我的建议是加入日期,然后使用条件聚合进行计算:

    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;
    

    【讨论】:

    • 谢谢,虽然它运行了,但是在我得到任何输出之前它需要很长时间并且超时。有更好的方法吗?
    猜你喜欢
    • 2021-07-13
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多