Retrieve the average of the values of a given column.

@author Morten Rugaard <moru@nodes.dk>

@param  string $column
@return float|int
16906
public function avg($column)
    {
        $result = $this->getBuilder()->avg($column);

        // Reset query builder
        $this->resetBuilder();

        return $result;
    }
970935
private static function generateFloatInput($column)
    {
        $fieldInput = $column->getName().':float,'.$column->getPrecision().','.$column->getScale();

        return $fieldInput;
    }
894529
public function parseColumn ($column)
	{
		
		// Set attribute
		
		$attribute	=	array ();
		
		// If the datatype can be parsed and the datatype is in the column map
		
		if (preg_match ('/^(\w*)(\((.*?)\))*(.*)$/', $column[1], $dataType) && 
		isset (self::$columnMap[$dataType[1]]))
		{
			
			// Set column map
			
			$columnMap				= self::$columnMap[$dataType[1]];
			
			// Set type
			
			$attribute['type']		= $columnMap[0];
			
			// Charset based on the type
			
			$charsetName			= $dataType[1];
			
			if (strpos ($dataType[4], 'unsigned') !== FALSE)
			{
				
				if ($this->parser->charsetExists ($charsetName . '_unsigned'))
				{
					$charsetName	.= '_unsigned';
				}
			}
			
			if ($this->parser->charsetExists ($charsetName))
			{
				$attribute['charset']	= $charsetName;
			}
			
			// No charset from the column data type so type the attribute type
			
			if (!isset ($attribute['charset']))
			{
				
				$charsetName			= str_replace ('self::TYPE_', '', $columnMap[0]);

				if (strpos ($dataType[4], 'unsigned') !== FALSE)
				{

					if ($this->parser->charsetExists ($charsetName . '_unsigned'))
					{
						$charsetName	.= '_unsigned';
					}
				}

				if ($this->parser->charsetExists ($charsetName))
				{
					$attribute['charset']	= strtolower ($charsetName);
				}
				
			}
			
			// Switch type
			
			switch ($attribute['type'])
			{
				
				case 'self::TYPE_INT':
					
					// Set column min and max
					
					if (isset ($columnMap[1]) && isset ($columnMap[2]))
					{
						
						// If the column is unsigned

						if (strpos ($dataType[4], 'unsigned') !== FALSE)
						{

							// Set min and max to unsigned

							$attribute['min']	= $columnMap[1] . '_UNSIGNED';
							$attribute['max']	= $columnMap[2] . '_UNSIGNED';

						}

						// Else the column is signed

						else
						{

							// Set min and max

							$attribute['min']	= $columnMap[1];
							$attribute['max']	= $columnMap[2];

						}
						
					}
					
					// Set default
					
					$attribute['default']	= $column[4]?: 0;
					
					// Break
					
					break;
				
				
				case 'self::TYPE_STRING':
					
					// Default charset
					
					$attribute['charset']	= 'default';
					
					// Set column min and max
					
					if (isset ($columnMap[1]) && isset ($columnMap[2]))
					{

						$attribute['min']	= $columnMap[1];
						$attribute['max']	= $columnMap[2];
						
					}
					
					// If value constaints have been set
					
					if ($dataType[3])
					{
						
						// Set max
						
						$attribute['max']	= (int)$dataType[3];
						
					}
					
					// Set default
					
					$attribute['default']	= '\'' . ($column[4]?: '') . '\'';
					
					// Break
					
					break;
				
				
				case 'self::TYPE_DECIMAL':

					// Set column min and max
					
					if (isset ($columnMap[1]) && isset ($columnMap[2]))
					{
						
						// If the column is unsigned

						if (strpos ($dataType[4], 'unsigned') !== FALSE)
						{

							// Set min and max to unsigned

							$attribute['min']	= $columnMap[1] . '_UNSIGNED';
							$attribute['max']	= $columnMap[2] . '_UNSIGNED';

						}

						// Else the column is signed

						else
						{

							// Set min and max

							$attribute['min']	= $columnMap[1];
							$attribute['max']	= $columnMap[2];

						}
						
					}
					
					// If value constaints have been set
					
					if ($dataType[3])
					{
						
						// Break constraint

						$constraint	= explode (',', $dataType[3]);

						// If the column is unsigned

						if (strpos ($dataType[4], 'unsigned') !== FALSE)
						{

							// Set max

							$attribute['max']	= (float)('1' . str_repeat ('0', ($constraint[0] - $constraint[1])));
							
						}
						
						// Else signed value
						
						else
						{
							
							// Set min and max
							
							$attribute['min']	= (float)('-1' . str_repeat ('0', ($constraint[0] - $constraint[1])));
							$attribute['max']	= (float)('1' . str_repeat ('0', ($constraint[0] - $constraint[1])));
							
						}
						
					}
					
					// Set default
					
					$attribute['default']	= $column[4]?: 0;
					
					// Break
					
					break;
				
				
				case 'self::TYPE_ENUM':
					
					// Remove '' from values
					
//					$values	= substr (str_replace ('\',\'', ',', $dataType[3]), 1, -1);
					
					// Set values
					
					$attribute['values']	= $dataType[3];
					
					// Set column min and max
					
					if (isset ($columnMap[1]) && isset ($columnMap[2]))
					{

						$attribute['min']	= $columnMap[1];
						$attribute['max']	= $columnMap[2];
						
					}
					
					// Set default
					
					$attribute['default']	= '\'' . (isset ($column[4])? $column[4] : '') . '\'';
					
					// Break
					
					break;
				
				
				default:
					
					// Set column min and max
					
					if (isset ($columnMap[1]) && isset ($columnMap[2]))
					{

						$attribute['min']	= $columnMap[1];
						$attribute['max']	= $columnMap[2];
						
					}
					
					// Set default
					
					$attribute['default']	= '\'' . ($column[4]?: '') . '\'';
					
					// Break
					
					break;
				
				
			}

			// If the column is the primary key
			
			if ($column[3] == 'PRI')
			{

				// Set min to 1 (required)

				$attribute['min']		= 1;

			}
			
			// Set NULL

			if ($column[2] == 'YES')
			{
				
				$attribute['null']		= 'TRUE';

				if (!isset ($attribute['default']) || !$attribute['default'] || $attribute['default'] == '\'\'')
				{
					$attribute['default']	= 'NULL';
				}

			}
			
		}
		
		// Return attribute array
		
		return $attribute;
		
	}
687307
public function count(string $column = '*'): int
    {
        $select = clone $this;

        //To be escaped in compiler
        $select->columns = ["COUNT({$column})"];
        $select->ordering = [];
        $select->grouping = [];

        $st = $select->run();
        try {
            return (int)$st->fetchColumn();
        } finally {
            $st->close();
        }
    }
1090879
public function max(string $column): int
    {
        $max = $this->queryRelate->getQuery()->max($column);

        $this->resetQueryRelate();

        return $max;
    }
941027
public function fetchField($column)
    {
        if ($this->rowCount == 0) {
            throw new Exception('Empty result set');
        }
        $row = $this->current();
        if (!isset($this->columnOids[$column])) {
            throw new Exception("Field '$column' not found");
        }

        return $row[$column];
    }
1075019
public function sum(string $column): int
    {
        $sum = $this->queryRelate->getQuery()->sum($column);

        $this->resetQueryRelate();

        return $sum;
    }
764377
public function getAutoIncrementValue(string $table, string $column = 'id')
    {
        $query = $this->connection->createQueryBuilder();
        $query->select($this->connection->getDatabasePlatform()->getMaxExpression($column) . ' AS id')
            ->from($table);

        $data = $query->execute()->fetchAll(PDO::FETCH_ASSOC);

        return (int) ($data[0]['id'] ?? 0) + 1;
    }
390525
protected function getDataColumn($column) {
		if (function_exists('array_column')) {
			return array_column($this->_data, $column);
		}
		
		$columnData = [];
		
		foreach ($this->_data as $row) {
			$columnData[] = $row[$column];
		}
		
		return $columnData;
	}
669046
public function sum($column)  {
        $val = $this->_selectFunction(__FUNCTION__, $column)->scalar();
        return empty($val) ? 0 : $val;
    }
1026187
public function count($column = NULL)
	{
		if ($column) {
			return $this->sum($column);
		}

		if ($this->result) {
			return count($this->result);
		}

		list(, $criteria, $options) = $this->queryBuilder->buildSelectQuery();

		return $this->query->count($this->name, $criteria, $options);
	}
1082481
public function getCountForPagination($column = '*')
	{
		$query = $this
			->cloneWithout(['columns', 'orders', 'limit', 'offset'])
			->cloneWithoutBindings(['select', 'order']);

		$columns = $this->withoutSelectAliases([$column]);

		// Once we have run the pagination count query, we will get the resulting count and
		// take into account what type of query it was. When there is a group by we will
		// just return the count of the entire results set since that will be correct.
		if(isset($this->groups) || count($query->havings))
		{
			$column = '*';
			if( !$columns[0] instanceof Expression && strpos($columns[0], '*') === false )
			{
				$columns[0] .= ' as uid';
				$column = 'uid';
			}

			$query = $this
				->newQuery()
				->fromSub( $query->select($columns), 'table_count' );

			$columns = [$column];
		}

		$raw = $query
			->setAggregate('count', $columns)
			->getRaw();

		if(!count($raw))
		{
			return 0;
		}

		$raw = $raw[0];
		if(is_object($raw))
		{
			return (int) $raw->aggregate;
		}
		else
		{
			return (int) array_change_key_case((array) $raw)['aggregate'];
		}
	}
894496
public function value($column = 0)
    {
        if (is_int($column)) {
            return $this->statement->fetchColumn($column);
        }

        return $this->row()[$column];
    }
859696
public function values($column = 0)
    {
        if (is_int($column)) {
            return $this->statement->fetchAll(PDO::FETCH_COLUMN, $column);
        }

        return array_map(function ($row) use ($column) {
            return $row[$column];
        }, $this->rows());
    }
1194103
protected function _getFloatColumn(Column\Float $column)
    {
        if (is_null($column->getDecimal())) {
            return sprintf(
                '%s FLOAT(%s)',
                $column->getName(),
                $column->getDigits()
            );
        }

        return sprintf(
            '%s DECIMAL(%s, %s)',
            $column->getName(),
            $column->getDigits(),
            $column->getDecimal()
        );
    }
718745
public function count($column = null){
		if($column === null){
			$this->performed || $this->run();
			return (int) $this->connection->getDriver()->getNumRows($this->resultSet);
		}
		return $this->aggregation("COUNT(:$column)");
	}
224299
public function column($column)
    {
        return $this
            ->map(function (CollectionableInterface $entity) use ($column) {
                return $this->getFieldValue($entity, $column);
            })
            ->toArray()
        ;
    }