--- src/libs/zbxserver/expression.c.orig 2011-04-15 21:17:29.000000000 +0200 +++ src/libs/zbxserver/expression.c 2011-05-03 12:34:13.000000000 +0200 @@ -958,7 +958,7 @@ if (FAIL == trigger_get_N_functionid(trigger, N_functionid, &functionid)) goto fail; - result = DBselect("select i.itemid,i.value_type from items i,functions f" + result = DBselect("select i.itemid,i.value_type,lastclock from items i,functions f" " where i.itemid=f.itemid and f.functionid=" ZBX_FS_UI64, functionid); @@ -969,9 +969,9 @@ if (value_type == ITEM_VALUE_TYPE_LOG) { zbx_snprintf(sql, sizeof(sql), "select %s from history_log" - " where itemid=%s order by id desc", + " where itemid=%s and clock=%s order by id desc", fieldname, - row[0]); + row[0], row[2]); h_result = DBselectN(sql, 1); @@ -1025,7 +1025,7 @@ goto fail; result = DBselect( - "select i.itemid,i.value_type,i.valuemapid,i.units,i.lastvalue" + "select i.itemid,i.value_type,i.valuemapid,i.units,i.lastvalue,i.lastclock" " from items i,functions f" " where i.itemid=f.itemid" " and f.functionid=" ZBX_FS_UI64, @@ -1040,9 +1040,9 @@ { case ITEM_VALUE_TYPE_LOG: case ITEM_VALUE_TYPE_TEXT: - zbx_snprintf(tmp, sizeof(tmp), "select value from %s where itemid=%s order by id desc", + zbx_snprintf(tmp, sizeof(tmp), "select value from %s where itemid=%s and clock=%s order by id desc", value_type == ITEM_VALUE_TYPE_LOG ? "history_log" : "history_text", - row[0]); + row[0], row[5]); h_result = DBselectN(tmp, 1); --- src/libs/zbxserver/evalfunc.c.orig 2011-04-15 21:17:29.000000000 +0200 +++ src/libs/zbxserver/evalfunc.c 2011-05-03 13:46:18.000000000 +0200 @@ -1768,13 +1768,12 @@ else { zbx_snprintf(sql, sizeof(sql), - "select value" - " from %s" - " where itemid=" ZBX_FS_UI64 - " order by %s desc", + "select h.value" + " from %s as h, items as i" + " where i.itemid=" ZBX_FS_UI64 + " and h.itemid=i.itemid and h.clock=i.lastclock", get_table_by_value_type(item->value_type), - item->itemid, - get_key_by_value_type(item->value_type)); + item->itemid); result = DBselectN(sql, arg2); }