Allows to calculate the average value on a column matching the specified conditions

@param string $field =array_keys(static::sample())[$i]
@param array  $filters =static::sample()

@return float|null
328747
public static function avg($field, $filters = null)
    {
        return (float)static::query()->where($filters)->avg($field);
    }
1086265
public function _prepareAsFilterValue()
    {
        if (is_null($this->_fields)) {
            $queryset = $this->asArray(['pk']);
        } else {
            if (count($this->_fields) > 1) {
                throw new TypeError('Cannot use multi-field values as a filter value.');
            }
            $queryset = $this->_clone();
        }

        return $queryset->query->toSubQuery($queryset->connection);
    }
1026868
public function count($field='*')
    {
        $this->getQuery()->type('select')->remove('fields')->set('count', $field);
        
        $result = $this->query();
        
        if ($result) {// get first row's value
            $result = current($result[0]);
        } else {
            $result = 0;
        }
        
        return $result;
    }
1336298
public function total($field = '*') {
        if (!$this->countperformed || $this->whereData) {
            $this->performCount('*');
        }

        return $this->count;
    }
1326948
public function avg($table, $field, $conditions = false)
    {
        return $this->executeAggregate('AVG', $table, $field, $conditions);
    }
411268
public function whereGreaterThan($column, $value, $filter = false)
    {
        return $this->where($column, '>', $value, $filter);
    }
952155
public function findWhereNotIn($field, array $values, $columns = ['*'])
    {
        $this->applyCriteria();
        $this->applyScope();
        $model = $this->model->whereNotIn($field, $values)->get($columns);
        $this->resetModel();

        return $this->parserResult($model);
    }
1263111
public function average($alias, $field = null, $script = null)
    {
        $aggregation = new AvgAggregation($alias, $field, $script);

        $this->append($aggregation);
    }
460823
protected function aggregate(string $type, string $field = '*'): float
    {
        // Verify that field exists
        if ($field !== '*' && isset($this->statements['selects']) === true && \in_array($field, $this->statements['selects'], true) === false) {
            throw new ColumnNotFoundException(sprintf('Failed to count query - the column %s hasn\'t been selected in the query.', $field));
        }

        if (isset($this->statements['tables']) === false) {
            throw new Exception('No table selected');
        }

        $count = $this
            ->table($this->subQuery($this, 'count'))
            ->select([$this->raw(sprintf('%s(%s) AS `field`', strtoupper($type), $field))])
            ->first();

        return isset($count->field) === true ? (float)$count->field : 0;
    }
624225
public function andWhereEqualsOrGreaterThan($column, $value, $filter = false)
    {
        return $this->andWhere($column, '>=', $value, $filter);
    }
23507
public function count(array $filter = [], array $options = [])
    {
        $cursor = $this->executeCommand(
            ['count' => $this->collectionName, 'query' => (object) $filter],
            $options
        );

        $result = $cursor->current();
        if (!isset($result['n'])) {
            throw new UnexpectedResultException('Command "count" did not return expected "n" field.');
        }

        return (int) $result['n'];
    }
170244
private function getValues(array $field)
    {
        if ($field['type'] !== 'select') {
            return null;
        }
        $field = Bag::from($field);

        // Get the appropriate values
        return is_string($field->get('values', [])) ? $this->getEntityValues($field) : $this->getYamlValues($field);
    }
453752
public static function value($filters, $field, $ttl = null)
    {
        if (!is_string($field)) {
            throw new ParameterOrderException(__METHOD__ . ' field');
        }

        if ($ttl !== null && !is_int($ttl)) {
            throw new MisuseException('ttl must be a integer');
        }

        $model = new static;
        $pkName = $model->getPrimaryKey();

        $pkValue = null;
        if (is_scalar($filters)) {
            $pkValue = $filters;
            $filters = [$pkName => $pkValue];
        } elseif (is_array($filters)) {
            if (count($filters) === 1 && isset($filters[$pkName])) {
                $pkValue = $filters[$pkName];
            }
        }

        if ($ttl === null || $pkValue === null) {
            $rs = static::query(null, $model)->select([$field])->where($filters)->limit(1)->fetch(true);
            return $rs ? $rs[0][$field] : null;
        }

        $key = '_mp:models:value:' . $model->getSource() . ":$field:$pkValue:$ttl";
        if (($value = $model->_di->ipcCache->get($key)) !== false) {
            return $value;
        }

        $rs = static::query(null, $model)->select([$field])->whereEq($pkName, $pkValue)->limit(1)->fetch(true);
        $value = $rs ? $rs[0][$field] : null;

        $model->_di->ipcCache->set($key, $value, $ttl);

        return $value;
    }
1052485
public function filter(array $filters)
	{
		foreach ($filters as $filter) {
			if ($filter->isValueSet()) {
				if ($filter->hasConditionCallback()) {
					Callback::invokeArgs(
						$filter->getConditionCallback(),
						[$this->data_source, $filter->getValue()]
					);
				} else {
					if ($filter instanceof Filter\FilterText) {
						$this->applyFilterText($filter);
					} elseif ($filter instanceof Filter\FilterMultiSelect) {
						$this->applyFilterMultiSelect($filter);
					} elseif ($filter instanceof Filter\FilterSelect) {
						$this->applyFilterSelect($filter);
					} elseif ($filter instanceof Filter\FilterDate) {
						$this->applyFilterDate($filter);
					} elseif ($filter instanceof Filter\FilterDateRange) {
						$this->applyFilterDateRange($filter);
					} elseif ($filter instanceof Filter\FilterRange) {
						$this->applyFilterRange($filter);
					}
				}
			}
		}

		return $this;
	}
403335
public function count($field = null)
    {
        // when no field is given we use *
        if (is_null($field))
        {
            $field = new Expression('*');
        }

        // return the column
        return (int) $this->column(new Func('count', $field));
    }
957005
public function substractValue($values)
    {
        if (isset($values[$this->id]) && ! Cuztom::isEmpty($values[$this->id])) {
            if (is_array($values[$this->id])) {
                $value = isset($values[$this->id][0]) ? maybe_unserialize($values[$this->id][0]) : null;
            } else {
                $value = maybe_unserialize($values[$this->id]);
            }
        } else {
            $value = $this->default_value;
        }

        return apply_filters('cuztom_field_value', $value, $this);
    }
37378
public function avg($field) {
        $this->statementContext->setFields("AVG({$field}) AS __AVG__");
        $row = $this->findOne();

        //SQL执行发生错误
        if ($row === false) {
            return false;
        }

        return $row['__AVG__'];
    }