2

我有一个找不到答案的问题。

基本上,我有两列:

variable 10  || 14  || 18 || 20 || 29  || 38 || 39  || 41 || 46  ...
values   857 || 736 || 84 || 1  || 362 || 74 || 183 || 77 || 944 ...

我想输出每个变量中所有值的总和,然后是 10s,(10, 14, 18)然后是 20s (20, 29),然后是 30s (38, 39),依此类推。

我将如何构建查询?

4

1 回答 1

4

尝试这个:

select variable div 10, sum(values) 
  from myTable
 group by variable div 10

结果(假设您的列已命名varval现在:

mysql> select var div 10, sum(val) from my  group by var div 10;
+------------+----------+
| var div 10 | sum(val) |
+------------+----------+
|          1 |     1677 | 
|          2 |      363 | 
|          3 |      257 | 
|          4 |     1021 | 
+------------+----------+
4 rows in set (0.00 sec)
于 2012-10-03T20:26:42.117 回答