# Reporting Calculation Help

I have a transaction group that runs on a schedule and logs the values every 15 minutes for Energy Delivered kWh and Energy Delivered kVAr. I have the values displaying on a a report with a Date and Timestamp, but I am needing to add another column that will run the below calculation. What would be the best way for me to go about doing this?

For the real power integrated demand, every time we query the kWh delivered, we would then calculate the demand by subtracting the reading from 15 minutes earlier and then multiplying the result by 4 (there are four, 15 minute intervals per hour). The same procedure applies to the kVAr delivered. As an example –
At 13:45 the delivered kWh reading is 2846.88 and the delivered kVArh reading is 722.66
At 14:00 the delivered kWh reading is 2882.19 and the delivered kVArh reading is 735.22

The calculation would be as follows for kW demand:
[The kWh reading at 14:00 minus the kWh reading at 13:45] * 4 or (2882.19 – 2846.88)*4 = (35.31)*4 = 141.24 kW demand for the period ending at 14:00.

Similarly, the calculation for kVAr demand would be:
[The kVArh reading at 14:00 minus the kVArh reading at 13:45] * 4 or (735.22 – 722.66)*4 = (12.56)*4 = 50.24 kVAr demand for the period ending at 14:00.

Any help would be greatly appreciated. Thanks.

You can do the calculation right in your SQL query. This Stack Overflow question will show you how to get the difference between the rows. Once you have that difference, SQL can also multiply it by 4.

I have 6 Different Meters logging every 15 minutes to the same Database. Each meter has an ID 1-6 so I need to only calculate (Current value for ID 1) - (Previous value from ID 1) and (Current value for ID 2) - (Previous value from ID 2). I cannot simply subtract the previous row in the database since they are not in order…So that is the part I am confused on. I have included a picture of the DB Query.

The answer by Quassnoi in the Stack Overflow thread will do what you want. Even though your database isn’t in the order you want, by doing the calculation off a subquery, you have complete control over the order of the subquery.