Sorry if this has been asked before. Let's imagine I have table of temperature measurements inside a set of mechanical components:
ComponentID | Timestamp | Value |
---|---|---|
A | 1st Jan 2020 00:00 | 20 C |
A | 1st Jan 2020 00:10 | 25 C |
B | 1st Jan 2018 00:00 | 19C |
...and so on. Size of the table is fairly big, i.e. I have thousands of components with 10-minute measurements over a couple of years. What I need is a tally of the temperatures for each component in each year into, say, 5-degree bins, so a table looking like this:
ComponentID | Year | [-20;-15) | [-15,-10) | [-10;-5) | ... |
---|---|---|---|---|---|
A | 2018 | 5 | 20 | 300 | ... |
A | 2019 | 0 | 41 | 150 | ... |
B | 2018 | 60 | 10 | 1 | ... |
..so for each component in each year, I count the number of measurements where the temperature has been in the [-20,-15) range, the number of measurements in the [-15,-10) range, and so on. I have a query doing this, but it's awfully slow. Is there an 'optimal' way of doing this kind of aggregation?