qbo
qbo

Reputation: 386

Get sum of column with condition

I have a question that I cannot find answer to.

Basically, I have two columns:

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

I want to output the sum of all the values in each variable with 10s (10, 14, 18) then 20s (20, 29) then 30s (38, 39) so on and so forth.

How would I construct the query?

Upvotes: 2

Views: 145

Answers (1)

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181280

try this:

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

result (assuming your columns are named var and val now:

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)

Upvotes: 4

Related Questions