简体   繁体   English

GROUP BY中的SQL计数行

[英]SQL Count rows in GROUP BY

The following query returns the following results: 以下查询返回以下结果:

SELECT sales.users_id, users.region
FROM sales
JOIN users ON ( sales.users_id = users.id )
GROUP BY sales.users_id

users_id, region
1,    IE
2,    UK
3,    UK
4,    UK
5,    AU
6,    AU

QUESTION: 题:

How do I modify the above query to get the following results: 如何修改上面的查询以获得以下结果:

IE, 1
UK, 3
AU, 2

Which is the number of users who purchased per region. 每个区域购买的用户数量。 Where the numbers in the second column correspond to the count of sales per region? 第二栏中的数字与每个区域的销售数量相对应?

Thanks in advance... 提前致谢...

SELECT users.region, count(*)
FROM sales
JOIN users ON ( sales.users_id = users.id )
GROUP BY users.region;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM