Skip to content
Advertisement

How to calculate power consumption from power records?

I have table which contains power values (kW) for devices. Values are read from each device once a minute and inserted into table with timestamp. What I need to do is calculate power consumption (kWh) for given time span and return 10 most power consuming devices. Right now I query results for given time span and do calculation in backend looping all records. This works fine with small amount of devices and with short time span, but in real use case I could have thousands of devices and long time span.

So my question is how could I do this all in PostgreSQL 9.4.4 so that my query would return only 10 most power consuming (device_id, power_consumption) pairs?

Example table:

Simple data example:

Wanted results for query:

Simplified example (created_at in hours) how I calculate kWh value:

EDIT: Check this to see how I ended up solving this.

Advertisement

Answer

If someone happens to wonder same thing here is how I solved this.
I followed instructions by David and made this:

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