0

这是指我之前提出的问题,并得到了一个非常快速的答案(在 sql 查询中的最大计数)。问题集是相似的,但是上一个问题中的解决方案将迫使我在循环中访问数据库,这将导致性能问题。所以我现在拥有的是,经过一些加入后:

    id | description
     0 | bla
     0 | blub
     0 | bla
     1 | blablub
     1 | bla
   ... | ...

如您所见,现在 id 不再是主键。我想要的是为结果集中的每个 id 获取最常用的描述。它应该看起来像这样:

 id | most_popular_description | times_the_desc_appeared_for_an_id
  0 |                      bla |                                 2
  1 |                  blablub |                                 1
... |                      ... |                               ...
4

3 回答 3

1

这应该可以解决问题。

select id, description, COUNT(description)
from mytable
group by id, description
order by 3 desc
于 2009-04-27T05:58:50.393 回答
1

如果您只想要最受欢迎的商品,那么我相信这应该会为您提供您正在寻找的结果集。还有其他方法可以做到这一点,但stats_mode是获得组中“最普遍”值的最简单方法(即模式)。

SELECT t.id,
       t.description AS most_popular_description,
       COUNT(*) AS times_the_desc_appeared_for_an_id
FROM mytable t INNER JOIN (
  SELECT id, stats_mode(description) AS desc FROM mytable GROUP BY id
) a ON t.id = a.id AND t.description = a.desc
GROUP BY t.id, t.description;

请注意,嵌套查询(内联视图)是必要的,因为您还需要计数。

于 2009-04-27T06:42:51.973 回答
0

我认为您可以使用 dense_rank() 分析函数来获取每个组集的前 N ​​个。

像这样的东西:

select id, description, times_the_desc_appeared_for_an_id
from
(
  select id, description, count(description) times_the_desc_appeared_for_an_id
  dense_rank() over (partition by id, description order by count(description) desc) position
  from mytable
  group by id, description
)
where
  position <= 3
order by id, times_the_desc_appeared_for_an_id;
于 2009-05-03T15:44:57.703 回答