Psyplant Psytrance
Psyplant Psytrance

Reputation: 23

how to sum a list of max values group by

I'm trying to sum the max values of a column named value1 grouped by sensor id value so I want the last greater value from each sensor id.

Using this code I get a column with rows with the max results for each sensor. But I dont know how to sum this values.

select max(value1) from `digiwork_esp-data`.SensorData group by sensor

I need to sum this individual values of the result into a total. I tried this, but it is returning a big number (not correct):

select sum(value1) 
from `digiwork_esp-data`.SensorData
where value1 = any (SELECT max(value1) FROM `digiwork_esp-data`.SensorData group by sensor)

Upvotes: 2

Views: 62

Answers (1)

Ed Bangga
Ed Bangga

Reputation: 13006

You can directly sum the result of the subquery.

select sum(t1.val) from
    (select max(value1) val FROM digiwork_esp-data.SensorData group by sensor) t1

Upvotes: 1

Related Questions