SQL Group by Count of Counts

2020-08-21 04:31发布

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.

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?

3条回答
该账号已被封号
2楼-- · 2020-08-21 05:05

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]
查看更多
Bombasti
3楼-- · 2020-08-21 05:17
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]
查看更多
The star\"
4楼-- · 2020-08-21 05:27

mysql> create table order1(counter int(3) not null auto_increment primary key, -> tag int(3)); Query OK, 0 rows affected (0.01 sec)

mysql> insert into order1 values(500,20);//insert somany values as u required and check insert into order1 values(500,20); insert into order1 values(600,20); mysql> select counter,count(*) from order1 group by counter

查看更多
登录 后发表回答