如何在同一SELECT语句中使用COUNT和GROUP BY我有一个SQL SELECT查询,其中有一个组BY。我想通过陈述来统计组后的所有记录。是否有一种直接从SQL进行此操作的方法?例如,有一个具有用户的表,我希望选择不同的城镇和共计用户数select town, count(*) from usergroup by town我希望有一个列包含所有城镇,另一个列有所有行的用户数。共有3个城镇和58个用户的结果如下:Town CountCopenhagen 58NewYork 58Athens 58
3 回答
慕码人2483693
TA贡献1860条经验 获得超9个赞
select town, count(town) from usergroup by town
GROUP BY
.
更新
DECLARE @numOfUsers INTSET @numOfUsers = SELECT COUNT(*) FROM userSELECT DISTINCT town, @numOfUsersFROM user
莫回无
TA贡献1865条经验 获得超7个赞
/* 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
添加回答
举报
0/150
提交
取消