我有一个查询,返回列和频率的最频繁值。(我已经删除了这个查询的内容,这使得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
结果
问题 | 频率 | 你好 | 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);