SQL:仅选择第二列最常使用的值为某些值的ID(按ID分组)

时间:2018-08-10 15:46:27

标签: sql postgresql

我有一个SQL表(使用postgres),其中有两列:videoid,分类

videoid | classification
1       |20
1       |24
1       |24
1       |24
1       |24
2       |20
2       |20
2       |20
2       |20
2       |24
2       |24
3       |24
3       |24

我正在尝试检索最常见分类为24的所有videoid。(答案应仅为videoid 1和3) 当我使用查询时: (从How to select most frequent value in a column per each id group?找到)

    SELECT DISTINCT ON (videoid) videoid, most_frequent_species FROM (
  SELECT videoid, classification AS most_frequent_species, count(*) as _count
FROM userviewed
GROUP BY videoid, classification) a
ORDER BY videoid, _count DESC
;

我检索结果集:

videoid     | most_frequent_species
    1       |24
    2       |20
    3       |24

但是当我尝试添加WHERE条款时:

WHERE classification = 24

我得到:

videoid| most_frequent_species
1      |24
2      |24
3      |24

如何创建只能检索的查询

videoid | most_frequent_species
1       |24
3       |24

2 个答案:

答案 0 :(得分:2)

您可以利用having子句(本质上是组后where子句)和mode函数:

select
    videoid
from
    userviewed
group by
    videoid
having
    mode() within group (order by classification) = 24

答案 1 :(得分:1)

一种方法使用distinct on,然后进行过滤:

select *
from (select distinct on (videoid) videoid, category, count(*)
      from userviewed
      group by videoid, category
      order by videoid, count(*) desc
     ) vc
where category = 24;