I have a table which stores consumed energy measurements. If I want to know the cost of energy I can select rows for given period, then select first and last row, calculate difference and multiply by price. The problem is if there are two tariffs and they switch from time to time:
kWh | tariff | time_stamp |
123 | G11 | t+0 |
130 | G11 | t+1 |
132 | G11 | t+2 |
140 | G12 | t+3 |
143 | G12 | t+4 |
150 | G11 | t+5 |
161 | G11 | t+6 |
I need to somehow group by tariff but I need new group every time the tariff changes. So in the above example there would be three groups, not two. There are only two tariffs possible, G11 and G12. Can I use GROUP BY or something else?