Index: api/classes/CHistory.php =================================================================== --- api/classes/CHistory.php (revision 28316) +++ api/classes/CHistory.php (working copy) @@ -141,6 +141,9 @@ $nodeCheck = true; $sqlParts['where'][] = DBin_node('h.itemid', $nodeids); } + if (!is_null($options['groupCount'])) { + $sqlParts['group']['h'] = 'h.itemid'; + } } // hostids @@ -158,6 +161,9 @@ $nodeCheck = true; $sqlParts['where'][] = DBin_node('i.hostid', $nodeids); } + if (!is_null($options['groupCount'])) { + $sqlParts['group']['i'] = 'i.hostid'; + } } // should be last, after all ****IDS checks @@ -197,7 +203,7 @@ // countOutput if (!is_null($options['countOutput'])) { $options['sortfield'] = ''; - $sqlParts['select'] = array('count(DISTINCT h.hostid) as rowscount'); + $sqlParts['select'] = array('count(h.itemid) as rowscount'); // groupCount if (!is_null($options['groupCount'])) { @@ -233,6 +239,7 @@ $sqlSelect = ''; $sqlFrom = ''; $sqlWhere = ''; + $sqlGroup = ''; $sqlOrder = ''; if (!empty($sqlParts['select'])) { $sqlSelect .= implode(',', $sqlParts['select']); @@ -243,6 +250,9 @@ if (!empty($sqlParts['where'])) { $sqlWhere .= implode(' AND ', $sqlParts['where']); } + if (!empty($sqlParts['group'])) { + $sqlWhere .= ' GROUP BY '.implode(',', $sqlParts['group']); + } if (!empty($sqlParts['order'])) { $sqlOrder .= ' ORDER BY '.implode(',', $sqlParts['order']); } @@ -252,14 +262,20 @@ ' FROM '.$sqlFrom. ' WHERE '. $sqlWhere. + $sqlGroup. $sqlOrder; $dbRes = DBselect($sql, $sqlLimit); $count = 0; $group = array(); while ($data = DBfetch($dbRes)) { - if ($options['countOutput']) { - $result = $data; - } + if (!is_null($options['countOutput'])) { + if (!is_null($options['groupCount'])) { + $result[] = $data; + } + else { + $result = $data['rowscount']; + } + } else { $itemids[$data['itemid']] = $data['itemid'];