按计数计数的 SQL 组

2021-09-10 00:00:00 sql tsql sql-server-2008 sql-server

这是我的剧本

SELECT COUNT( [Id]) as [Count Of Register],
Tag as [Tag]
FROM [Members]
Group By Tag 
Order By [Count Of Register] Desc;

返回的表是这样的:

Count   Tag

550 ----1
550 ----2
545 ----3
545 ----4
545 ----5

所以,这次我需要标签计数,按这个新的计数字段分组.

So, this time I need Count of Tag, Group by this new Count field.

一些返回值如:

2 ---550
3 ---545

有什么办法可以不使用新表或模板表或任何存储表,仅通过查询?

Is there any way without using new table or Template Table or any Storage Table just by query?

推荐答案

SELECT [Count Of Register], COUNT(1) [Grouped Count]
FROM
(
    SELECT COUNT( [Id]) as [Count Of Register],
           Tag as [Tag]
    FROM [Members]
    Group By Tag 

) MyTable
GROUP BY [Count Of Register]

相关文章