【问题标题】:SQL: only selecting id's where the most frequent value of a second column is a CERTAIN VALUE (Grouped by id)SQL:仅选择第二列的最常见值为 CERTAIN VALUE 的 id(按 id 分组)
【发布时间】:2018-08-10 15:46:27
【问题描述】:

我在 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

【问题讨论】:

  • 您可以将查询包装在子查询中并添加WHERE most_frequent_species = 24
  • 感谢 Giorgos 完美运行!

标签: sql postgresql


【解决方案1】:

您可以使用having 子句(本质上是一个post-group where 子句)和mode 函数:

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

【讨论】:

    【解决方案2】:

    一种方法使用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;
    

    【讨论】:

    • 非常感谢 Gordon,这非常有效!为我节省了很多时间
    猜你喜欢
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    相关资源
    最近更新 更多