2016-12-01 49 views
0

我想知道是否可以使用「count」函數或等同函數但指定了參數,只計算特定結果。帶參數的Esper計數

所以我想這樣做:

select 
    ... 
    "nbAccel", count(type='c8y_HarshBehavior' and behaviorType = "ACCELERATION") 
    "nbBraking", count(type='c8y_HarshBehavior' and behaviorType = "BRAKING") 
    "nbUnknown", count(type='c8y_HarshBehavior' and behaviorType = "UNKNOWN") 
from 
    ... 

讓我告訴你我想要什麼。我正在使用EPL Online tool

這裏是我的EPL報表(與我上面不工作粘貼的部分,淡然的):

create schema EventCreated(
    source String, 
    type String, 
    time Date, 
    behaviorType String 
); 

create schema CreateMeasurement(
    source String, 
    type String, 
    time Date, 
    fragments Object 
); 



@Name("no_message_20min") 
insert into 
    EventCreated 

select 
    e.source as source, 
    "c8y_SilentTracker" as type, 
    new Date() as time 

from pattern [ 
    every e = EventCreated(
     type != "c8y_HeartbeatReport" and 
     type != "c8y_ObdDisconnectionReport" and 
     type != "c8y_PowerOffReport" and 
     type != "c8y_SilentTracker") 

-> (timer:interval(20 minute) and 
    not EventCreated(
     source = e.source and 
     type != "c8y_HeartbeatReport"))]; 



@Name("create_context") 
create context Trip 
    context bySource 
     partition by source from EventCreated, 

    context byEvents 
     start EventCreated(
      type = "c8y_ObdConnectionReport" or 
      type = "c8y_PowerOnReport" or 
      type = "c8y_FixedReport" or 
      type = "c8y_HarshBehaviorReport") as startEvent 

     end EventCreated(
      type = "c8y_ObdDisconnectionReport" or 
      type = "c8y_PowerOffReport" or 
      type = "c8y_SilentTracker") as endEvent; 



@Name("context_end") 
context Trip 
    insert into 
     CreateMeasurement 

    select 
     context.bySource.key1 as source, 
     "Trip" as type, 
     e.time as time, 
     { 
      "startedBy", context.byEvents.startEvent.type, 
      "startedAt", context.byEvents.startEvent.time, 
      "endedBy", e.type, 
      "endedAt", e.time, 
      "nbAccel", count(type='c8y_HarshBehavior' and behaviorType = "ACCELERATION"), 
      "nbBraking", count(type='c8y_HarshBehavior' and behaviorType = "BRAKING"), 
      "nbUnknown", count(type='c8y_HarshBehavior' and behaviorType = "UNKNOWN") 
     } as fragments 

    from 
     EventCreated e 

    output 
     last when terminated; 

這裏是我的時間和事件順序:

EventCreated = { 
    source = 'tracker1', 
    type = 'c8y_ObdConnectionReport', 
    time = '2016-10-07T10:00:00.000' 
} 

t = t.plus(5 minutes) 

EventCreated = { 
    source = 'tracker1', 
    type = 'c8y_HarshBehavior', 
    time = '2016-10-07 10:05:00.000', 
    behaviorType = "UNKNOWN" 
} 

t = t.plus(5 minutes) 

EventCreated = { 
    source = 'tracker1', 
    type = 'c8y_HarshBehavior', 
    time = '2016-10-07 10:10:00.000', 
    behaviorType = "ACCELERATION" 
} 

t = t.plus(5 minutes) 

EventCreated = { 
    source = 'tracker1', 
    type = 'c8y_HarshBehavior', 
    time = '2016-10-07 10:15:00.000', 
    behaviorType = "BRAKING" 
} 

t = t.plus(5 minutes) 

EventCreated = { 
    source = 'tracker1', 
    type = 'c8y_Location', 
    time='2016-10-07 10:20:00.000' 
} 

t = t.plus(25 minutes) 

EventCreated = { 
    source = 'tracker1', 
    type = 'c8y_Location', 
    time = '2016-10-07 10:45:00.000' 
} 

t = t.plus(5 minutes) 

EventCreated = { 
    source = 'tracker1', 
    type = 'c8y_ObdDisconnectionReport', 
    time = '2016-10-07 10:50:00.000' 
} 

有了一個開始時間在2016-10-07 10:00:00.000

回答

0

好吧,我發現如何做到這樣的總和功能:

"nbAccel", coalesce(sum(1, type='c8y_HarshBehavior' and behaviorType = "ACCELERATION"), 0), 
"nbBraking", coalesce(sum(1, type='c8y_HarshBehavior' and behaviorType = "BRAKING"), 0), 
"nbUnknown", coalesce(sum(1, type='c8y_HarshBehavior' and behaviorType = "UNKNOWN"), 0) 
+0

好了,但是由於支持「count(color ='red')」,所以首先問題是什麼? – goodie

+0

只需將我的示例複製粘貼到EPL Online Tool中(也在上面的帖子中鏈接)並運行它,您將看到計數不起作用。我得到的'nbAccel'= 6,'nbBraking'= 6,'nbUnknown'= 6,我應該有1,1,1。看起來計數不知道如何應用參數並且不加區別地計算所有事件。上面的代碼使用總和,它雖然工作。 –

+0

這裏有太多的東西來看看爲什麼計數結果不是預期的。我建議刪除一些間接方法來查看計數是否正確。 – goodie