如何在同一SELECT语句中使用COUNT和GROUP BY我有一个SQL SELECT查询,其中有一个组BY。我想通过陈述来统计组后的所有记录。是否有一种直接从SQL进行此操作的方法?例如,有一个具有用户的表,我希望选择不同的城镇和共计用户数select town, count(*) from usergroup by town我希望有一个列包含所有城镇,另一个列有所有行的用户数。共有3个城镇和58个用户的结果如下:Town CountCopenhagen 58NewYork 58Athens 58
3 回答
慕桂英3389331
TA贡献2036条经验 获得超8个赞
select town, count(town) from usergroup by town
GROUP BY
.
更新
DECLARE @numOfUsers INTSET @numOfUsers = SELECT COUNT(*) FROM userSELECT DISTINCT town, @numOfUsersFROM user
慕工程0101907
TA贡献1887条经验 获得超5个赞
/* Number of rows in a derived table called d1. */select count(*) from( /* Number of times each town appears in user. */ select town, count(*) from user group by town) d1
- 3 回答
- 0 关注
- 2503 浏览
添加回答
举报
0/150
提交
取消