假设我有一张表,如下所示:
UserID Meh Meh //meh is some column
````````````````````
01 ... ...
01 ... ...
03 ... ...
05 ... ...
05 ... ...
01 ... ...
03 ... ...
所以我想计算每个用户ID在此表中出现的次数。我现在正在这样做:
select UserId from NinjaTable group by UserId
但它给了我一些我不懂或不懂的东西。 我希望结果如此:
UserID Frequency
```````````````````
01 3
03 2
05 2
答案 0 :(得分:3)
您正在分组,只需将计数添加到select子句。
select UserId, COUNT(*) from NinjaTable group by UserId order by COUNT(*) desc
应该真的这样做。
编辑:感谢@abe指点订单。
答案 1 :(得分:1)
尝试类似:
select UserId, count(*) as Frequency from NinjaTable group by UserId
如果你愿意,可以点击order by UserId
。
答案 2 :(得分:0)
尝试
select UserID, Count(*)
from theTable
group by UserID
答案 3 :(得分:0)
我认为你真正想要使用的是COUNT
函数:
SELECT UserID, COUNT(*) AS Frequency FROM NinjaTable GROUP BY UserID
答案 4 :(得分:0)
您可以使用group by语句:
SELECT UserId, COUNT(1) FROM MyTable GROUP BY UserId ORDER BY COUNT(1) DESC
这会将所有用户ID分组,找到行数(频率为你),然后按频率降序对结果集进行排序。