首页 > 解决方案 > 使用 group by 子句连接结果

问题描述

我如何加入以下 2 个查询 查询 1

select phn,count(*) from table 1 
group by phn
having count(*)>20 

有了上面的结果,我需要加入表 2 来获取 id

查询 2

select count(distinct id) from table 2 
where (result_of_query1).phn=table 2.phn

标签: sqlhivesubqueryinner-joinhaving-clause

解决方案


您可以通过以下方式加入:

select count(distinct id)
from table2 t2
inner join (
    select phn
    from table1 
    group by phn
    having count(*) > 20 
) t1 on t1.phn = t2.phn

推荐阅读