SQLite3计数多少个计数?

时间:2019-03-18 16:36:14

标签: database sqlite count

我正在学习SQLite3,并在使用此特定输出时遇到了麻烦。 假设我有一列:

user0
user1
user1
user2
user2
user3
user3
user3
user4
user4
user4
user4

我想计算用户出现在该列中的次数,并得到如下输出:

1 | 1
2 | 2
1 | 3
1 | 4

含义:1位用户出现1次,2位用户出现2次,1位用户出现3次,1位用户出现4次。

我不需要知道其他任何信息,只需知道有多少用户和多少个帐户。

2 个答案:

答案 0 :(得分:1)

group by一次获取所需的第一列的计数器,然后再次获取该结果:

select count(*) total, counter
from (
  select count(*) counter 
  from tablename
  group by col
)
group by counter

请参见demo
结果:

| total | counter |
| ----- | ------- |
| 1     | 1       |
| 2     | 2       |
| 1     | 3       |
| 1     | 4       |

答案 1 :(得分:0)

这是一个使用GROUP BY和COUNT()函数的小示例。 我正在使用MSSQL,但在SQLITE3中应该几乎相同。 这是我使用的表格:

enter image description here

select count([name]), [name] from Test
group by [name]

这是结果:

enter image description here

相关问题