Skip to content
Advertisement

Easy substraction of year’s values

I do have the following database table containing the timestamps in unix format as well as the total yield (summing up) of my solar panels every 5 mins:

Now I would like to calculate how much energy was produced each year. I thought of reading the first and last timestamp using UNION of each year:

This works fine but I need to do some post processing to substract end year’s value with the first year’s one. There must be a more elegant way to do this in SQL, right?

Thanks,

Anton

Advertisement

Answer

You can aggregate by year and subtract the min and max value:

This assumes that TotalYield does not decrease — which your question implies.

If you actually want the next year’s value, you can use LEAD():

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement