mysql - 查询分组并创建分组总计

标签 mysql sql group-by

我的 table 是这样的

id     views     country    
1       44        A
2       43        A
3       899       B
4       242       A
5       3204      B
etc.

I want to count how many times a country is occurring.

For example in above case country A is occurring 3 times. So for my query I want to be able to correctly show the how many times each record is occurring, but in addition to the count I want to add the views also. In above case I want that country A is occurring 3 times and with views as the sum of 44 + 43 + 242 = 329, but my query displays only the first views output: 44.

Here is my query

 SELECT count( * ) value, country, concat( country , '_', views ) prod 
 FROM tableA
 GROUP BY prod

最佳答案

SELECT country, COUNT(*), SUM(views) FROM tableA GROUP BY country

查看 sqlfiddle .

关于mysql - 查询分组并创建分组总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12872306/

相关文章:

mysql - 在 MySQL 中重新创建 SHOW PROFILES 变量的代码

mysql - 从 mysql 提取数据到 hadoop 时的连接问题

sql - 如何在oracle数据库中存储枚举字段类型的对象

mysql - 在 MySql 中执行查询时与 only_full_group_by 相关的错误

PHP PDO 参数编号无效。虽然所有参数都已设置

php - codeigniter order by desc 对于超过 2 或 3 位数字无法正常工作

mysql - SQL 查询返回双倍值

sql - 一次选择返回两个 Count(*) 结果

mysql - 如何按DAYOFWEEK分组并在MySQL中使用分组数据?

mysql - 选择在一台机器上工作而不是在另一台机器上工作时非按列分组