Skip to content
Advertisement

SQL SUM values in declared column into total and group by id

Hello people of the internet.

Once again I seek your advice.

I have multiple meter readers collecting power consumption every 5 minutes and storing to MS SQL Database. I execute SQL query to get a days worth of data from all meter readers and check how much power it has used every five minutes and returning as ‘

Consumed

‘ result. This is SQL syntax that I use to get data:

The result I Get is this:

Now I want to sum all ‘Consumed’ column data to as totalPower for each department, and group by Dep_Name in 1 day interval? Something Like this:

Is it possible to do so? if Yes, how can I do it?

I am new to all this, so I don’t know.

Advertisement

Answer

If T_Energy_A is always increasing, you can just do:

If you want this on a day by day basis:

Otherwise, you can just add one level of aggregation to your existing query:

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