2017-06-01 19 views
0

我試圖使用mode()或most_common_vals()函數作爲子查詢條件。PostgreSQL篩選其中類別是最常見的值

SELECT user_id, COUNT(request_id) AS total 
FROM requests 
WHERE category = (SELECT mode(category) AS modal_category FROM requests) 
GROUP BY user_id 
ORDER BY total DESC 
LIMIT 5; 

但是,我仍然收到關於兩個函數不存在的錯誤。

+0

如果數據是多模式的,您想要做什麼,即有兩個或多個「最常見」值? –

回答

0

如果我理解正確的,你需要的是這樣的:

with requests (user_id, request_id, category) as (
select 1, 111, 'A' union all 
select 1, 111, 'A' union all 
select 2, 111, 'A' union all 
select 2, 111, 'B' union all 
select 1, 111, 'B' union all 
select 3, 111, 'B' union all 
select 1, 111, 'B' union all 
select 1, 111, 'C' 
) 

-- Below is actual query: 

select user_id, COUNT(request_id) AS total 
from (
    select t.*, rank() over(order by cnt desc) as rnk from (
     select requests.*, count(*) over(partition by category) as cnt from requests 
    ) t 
) tt 
where rnk = 1 
group by user_id 
order by total desc 
limit 5 

這裏user_idCOUNT(request_id)只計算爲'B'類,因爲它是最常見的這個例子。

另請注意,如果存在多個最常見的類別,則該查詢會生成所有這些類別的結果。