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;
    }