ITEM | trigger | Fault/OK Cnt_Per_Item
|1 |2016-04-08 11:49:15.483 |Fault |5|
|2 |2016-04-08 12:49:15.383 |Fault |1|
|4 |2016-05-08 11:49:15.723 |Fault |1|
|2 |2016-07-09 00:49:15.503 |Fault |2|
|1 |2016-04-08 11:50:24.103 |OK |4|
|1 |2016-08-08 13:06:35.157 |Fault |5|
|1 |2016-08-11 13:06:35.277 |Fault |6|
答案 0 :(得分:1)
如果您有一张大桌子,这将效率不高。但是你可以用相关的子查询来做到这一点:
update t
set cnt_per_item = coalesce( (select sum(case when fault_ok = 'fault' then 1 else -1 end)
from t t2
where t2.item = t.item and t2.trigger <= t.trigger
), 0);