如何计算组返回的组的数量? [英] How to count the number of groups returned by a group by?

查看:164
本文介绍了如何计算组返回的组的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

select count(*) as count from table
group by foreign_id order by count 

这会返回每个外部标识的匹配数。

This returns a number of matches for each foreign id. However what im looking for is to summarise the results.

所以结果是:

10 results grouping 1 elements
5  results grouping 2 elements
7  results grouping 7 elements


推荐答案

好的,得到了​​。问题的标题比问题本身更好:)

Ok, got it. The title of the question expainls it better than the question itself :)

您需要先知道每个FK出现的次数:

You need to first know how many times each FK appears:

select count(*) as GroupAmount from t1
group by foreign_id

一旦你有了这一点,你必须分组,得到每个项目出现的次数与上述相同的方式。这将导致:

Once you have this, you have to group them to get the amount of times each item appears the same way as above. This will result in:

select GroupAmount, count(*) GroupAmountTimes from (
  select count(foreign_id) as GroupAmount from t1
  group by foreign_id
) as SubQuery
group by GroupAmount

查看操作此处

这篇关于如何计算组返回的组的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆