harp1814
harp1814

Reputation: 1658

MySQL: Get count for each range

There is mysql Ver 8.0.18 value_table as:

value count
1    3
11   1
12   2
22   5
31   1
34   3
35   1
40   3
46   7

What is query to get a total count for each dozen (1-10 - first dozen,11-20 - second , etc..) as:

1   3
2   3
3   5
4   8
5   7

Query should be flexible, so when some records added to value_table , for example

51 2
62 3

so, it is not necessary to change a query by adding new range (51-60 - 6-th dozen, etc.)

Upvotes: 1

Views: 89

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270081

I think you just want division and aggregation:

select min(value), sum(count)
from t
group by floor(value / 10);

To be honest, I'm not sure if the first column should be min(value) or floor(value / 10) + 1.

Upvotes: 2

Related Questions