Search code examples
espernesper

Filtering by aggregate function


I am trying to raise an event when the average value of a field is over a threshold for a minute. I have the object defined as:

class Heartbeat
{
    public string Name;
    public int Heartbeat;
}

My condition is defined as

select avg(Heartbeat) , Name
from Heartbeat.std:groupwin(Name).win:time(60 sec)
having avg(Heartbeat) > 100

However, the event never gets fired despite the fact that I fire a number of events with the Heartbeat value over 100. Any suggestions on what I have done wrong?

Thanks in advance


Solution

  • It confuses many people, but since time is the same for all groups you can simplify the query and remove the groupwin. The documentation note in this section explains why: http://esper.codehaus.org/esper-4.11.0/doc/reference/en-US/html_single/index.html#view-std-groupwin The semantics with or without groupwin are the same.

    I think you want group-by (and not groupwin) since group-by controls the aggregation level and groupwin controls the data window level.

    New query: select avg(Heartbeat) , Name from Heartbeat.win:time(60 sec) group by Name having avg(Heartbeat) > 100