Joining an event with a previous event
Another use case that is reasonably common is where an item output from a stream query needs to be compared to the previous output item. For example, suppose you need to detect for a given sensor when the average temperature value was below a threshold value but now is above the threshold value.
01. using com.apama.aggregates.mean; 
02. event Temperature { string sensorId; float temperature; } 
03. monitor DetectBreach { 
04.    action onload() { 
05.       stream<float> temperatures := all Temperature(sensorId="S001"); 
06.       stream<boolean> current := from t in temperatures within 60.0 
07.       select mean(t.temperatures) > 97.0; 
08.       stream<boolean> previous := from c in current retain 1 select rstream c; 
09.       string text; 
10.       from c in current from p in previous where c and not p 
11.       select "Temperature breach" : text { 
12.          print text; 
13.       } 
14.    } 
15. }