按计数计数的SQL组

时间:2011-12-01 08:50:04

标签: sql sql-server sql-server-2008 tsql

这是我的剧本

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

所以,这次我需要Count of Tag,Group by这个新的Count字段。

某些返回值如:

2 ---550
3 ---545

有没有办法不使用新表或模板表或任何存储表只是通过查询?

3 个答案:

答案 0 :(得分:15)

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]

答案 1 :(得分:1)

您可以使用

SELECT [Count Of Register], COUNT(*) FROM
    (SELECT COUNT([Id]) as [Count Of Register], Tag as [Tag]
     FROM [Members] GROUP BY Tag) q
GROUP BY [Count Of Register]

答案 2 :(得分:0)

的MySQL> create table order1(counter int(3)not null auto_increment primary key,      - > tag int(3)); 查询OK,0行受影响(0.01秒)

的MySQL>插入order1值(500,20); //按需要插入somany值并检查        插入order1值(500,20);        插入order1值(600,20); MySQL的>选择计数器,按计数器从order1组计数(*)