简体   繁体   English

按计数计数的SQL组

[英]SQL Group by Count of Counts

This is my script 这是我的剧本

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

Returned table is like this: 返回表是这样的:

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. 所以,这次我需要Count of Tag,Group by这个新的Count字段。

Some Returned Values Like: 一些返回值如:

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]

You could use 你可以用

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]

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

mysql> insert into order1 values(500,20);//insert somany values as u required and check insert into order1 values(500,20); mysql> insert into order1 values(500,20); //按需要插入somany值并检查插入到order1值(500,20); insert into order1 values(600,20); 插入order1值(600,20); mysql> select counter,count(*) from order1 group by counter mysql>选择计数器,从计数器的order1组计数(*)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM