Execute an aggregate function on the database.

@param  string $function
@param  array $columns
@return mixed
6876
public function aggregate( $function, $columns = ['*'] )
	{
		// Postgres doesn't like ORDER BY when there's no GROUP BY clause
		if ( !isset( $this->groups ) )
			$this->reOrderBy( null );

		$this->aggregate = compact( 'function', 'columns' );

		$previousColumns = $this->columns;

		// We will also back up the select bindings since the select clause will be
		// removed when performing the aggregate function. Once the query is run
		// we will add the bindings back onto this query so they can get used.
		$previousSelectBindings = $this->bindings['select'];

		$this->bindings['select'] = [];

		$results = $this->get( $columns );

		// Once we have executed the query, we will reset the aggregate property so
		// that more select queries can be executed against the database without
		// the aggregate value getting in the way when the grammar builds it.
		$this->aggregate = null;

		$this->columns = $previousColumns;

		$this->bindings['select'] = $previousSelectBindings;

		if ( isset( $results[0] ) )
		{
			$result = array_change_key_case( (array) $results[0] );

			return $result['aggregate'];
		}

		return null;
	}
907821
public function get($columns = ['*'])
    {
        $original = $this->columns;

        if (is_null($original)) {
            $this->columns = $columns;
        }

        $results = $this->runSelect();

        $this->columns = $original;

        return $results;
    }
1323419
protected function setAggregate($function, $columns)
    {
        $this->aggregate = compact('function', 'columns');

        if (empty($this->groups)) {
            $this->orders = null;

            $this->bindings['order'] = [];
        }

        return $this;
    }
800696
private function update($columns, $values)
    {
        $query = Query::table(static::$table);
        //
        for ($i = 0; $i < Table::count($columns); $i++) {
            $query = !empty($values[$i]) ? $query->set($columns[$i], $values[$i]) : $query->set($columns[$i], 'null', false);
        }
        //
        $query->where($this->_keyName, '=', $this->_keyValue)
        ->update();
        //
        return $query;
    }
129410
public static function columnSum(array $data, array $columns)
    {
        $result = array();

        foreach ($columns as $column) {
            foreach ($data as $collection) {
                if (isset($collection[$column])) {
                    if (isset($result[$column])) {
                        $result[$column] += $collection[$column];
                    } else {
                        $result[$column] = $collection[$column];
                    }
                }
            }
        }

        return $result;
    }
669046
public function sum($column)  {
        $val = $this->_selectFunction(__FUNCTION__, $column)->scalar();
        return empty($val) ? 0 : $val;
    }
663767
public static function select(array $columns = ['*'], ...$joinsColumns)
    {
        $query               = new static(Query\Type::SELECT);
        $query->columns['t'] = $columns;

        $alias = 'j1';
        foreach ($joinsColumns as $joinColumns) {
            $query->columns[$alias] = (array)$joinColumns;
            $alias++;
        }

        return $query;
    }
1234737
private function executeAgregat($aggregat, $column)
    {
        $sql = 'select ' . $aggregat . '(`' . $column . '`) from `' . $this->table . '`';

        if (!is_null($this->where)) {
            $sql .= ' where ' . $this->where;

            $this->where = null;
        }

        if (!is_null($this->group)) {
            $sql .= ' ' . $this->group;

            $this->group = null;

            if (!isNull($this->havin)) {
                $sql .= ' having ' . $this->havin;
            }
        }

        $s = $this->connection->prepare($sql);

        $this->bind($s, $this->where_data_binding);

        $s->execute();

        if ($s->rowCount() > 1) {
            return Sanitize::make($s->fetchAll());
        }

        return (int) $s->fetchColumn();
    }
429765
public function aggregate($function, $fields=null)
    {
        $query = clone $this->_query;

        return $query->select(sprintf('%s(%s)', $function, $fields))->execute()->scalar();
    }
257164
public function last($columns = array('*'))
    {
        $columns = is_array($columns) ? $columns : func_get_args();

        $results = $this->scrape($columns);

        return end($results);
    }
487991
public function alias(String $column, String $alias)
	{
		// if (!$this->getBinding('select') || empty($this->getBinding('select'))) {
			// Since this method cannot be used without the SELECT statement,
			// we will throw an exception if `SELECT` binding is null or false. 
			// throw new RuntimeException(sprintf('Cannot call aggregate function on empty select.'));
		// }
		$alias = str_replace($this->generator->getDisallowedChars(), '', $alias);
		return $this->attachSeparator($column . ' AS ' . $alias);
	}
36823
public function select($columns = [])
    {
        $expr = new ColumnExpression($this->getSQLStatement());

        if ($columns instanceof Closure) {
            $columns($expr);
        } else {
            if (!is_array($columns)) {
                $columns = [$columns];
            }
            $expr->columns($columns);
        }
    }
398930
public function get($columns = array('*'))
    {
        $columns = is_array($columns) ? $columns : func_get_args();

        return $this->scrape($columns);
    }
1078533
public function update($columns)
    {
        $update = array();
        foreach ($columns as $k => $v) {
            $update[] = "`$k` = :$k";
            $this->params[":$k"] = $v;
        }
        $update = implode(", ", $update);
        $q = "UPDATE {$this->table} as `t` {$this->join} SET $update WHERE {$this->condition}";
        if ($this->limit) {
            $q .= " LIMIT {$this->limit}";
        }
        return $this->connection->execQuery($q, $this->params);
    }
467409
public function get( $columns = [ '*' ] ) {
		return $this->onceWithColumns( $columns, function () {
			return $this->connection->fetchAll( $this->toSql(), $this->getBindings() );
		} );
	}
546208
public function count($columns = null)
    {
        $this->compileWheres();
        $result = $this->query->count();

        return (int) $result;
    }
977211
public function orHavingMultiple(array $columns)
    {
        $instance = $this->havingStrategyInstance();

        $instance->havingStrategy()->orHavingMultiple($columns);

        return $instance;
    }