如何根据另一个表最常见的值过滤列?



我有一个查询,返回列和频率的最频繁值。(我已经删除了这个查询的内容,这使得CTE很有帮助)

查询1

WITH data AS
(
SELECT question, COUNT(*) AS frequency
FROM DB1
GROUP BY 1
LIMIT 50
)
SELECT question, frequency
FROM data
ORDER BY frequency DESC

结果

tbody> <<tr>世界
问题频率
你好132140
120492
select user_id from db2 where question_id in (
select question_id from db1 group by question_id
having count(*) >= all (select count(*) from db1 group by question_id)
);

或者更现代的方式:

with freq as (
select question_id, dense_rank() over (order by count(*) desc) as dr
from db1 group by question_id
)
select user_id from db2
where question_id in (select question_id from freq where dr = 1);

相关内容

  • 没有找到相关文章

最新更新