Comment 1 for bug 1061817

Revision history for this message
Julien Danjou (jdanjou) wrote :

I've submitted the problem to a panel of SQL experts and they provided 2 implementations of a MAX() function for such a counter.

Simple
=====
This is probably the most portable approach, but it requires a double scan of the table, so it's not that efficient. It has been tested on MySQL.

SELECT SUM(IF(COALESCE((SELECT counter_volume FROM meter M2 WHERE M2.timestamp>M1.timestamp
      ORDER BY M2.TIMESTAMP ASC LIMIT 1),0)<,counter_volume,0)) FROM meter M1 ORDER BY timestamp
      ASC

Windowed
========
This uses window function and is more efficient, however I'm not sure it's available everywhere. It has been tested on PostgreSQL.

with t(tops) as (
  select case when lead(counter_volume) over w < counter_volume then counter_volume
               when lead(counter_volume) over w is null then counter_volume
               else null
           end as max
     from meter
   window w as (order by timestamp)
)
select sum(tops) from t;

(t returns the list of the higher values above all, and then we sum them to get max)

I think this is a good proof that this is definitely doable. For such counter, we likely want MAX() - FIRST() (to get the actual number of bytes sent via a NIC, for example ) and this is doable in an SQL query.