Build the `GROUP BY` clause.

@return string The `GROUP BY` clause.
506498
protected function _group()
    {
        $result = [];
        foreach ($this->_parts['group'] as $name => $value) {
            $result[] = $this->dialect()->name($name);
        }
        return $fields = join(', ', $result);
    }
86388
public function build()
    {
        $params = [];
        if ($this->value !== null) {
            $op = !is_null($this->operator) ? $this->operator : '=';

            $paramName = str_replace('.', '_', $this->key);
            if ($op == 'BETWEEN') {
                $sql = "{$this->key} $op ? AND ?";

                $between = explode('AND', $this->value);

                $params[':dateFrom'] = trim($between[0]);
                $params[':dateTo'] = trim($between[1]);
            } else {
                $sql = "{$this->key} $op ?";                                    // $sql = "{$this->key} $op {$paramName}";
                $params[":{$paramName}"] = $this->value;
            }
        } else {
            $sql = $sql = "{$this->key} IS NULL ";
        }

        return [$sql, $params];
    }
1186230
public function count()
    {
        if ($this->rowCount !== null) {
            return $this->rowCount;
        }

        $qbOriginal = $this->getQueryBuilder();
        $qb = clone $qbOriginal;

        $dqlParts = $qb->getDQLParts();
        $groupParts = $dqlParts['groupBy'];

        /*
         * Reset things
         */
        $qb->setFirstResult(null)
            ->setMaxResults(null)
            ->resetDQLParts([
            'orderBy',
            'select',
            ]);

        if (count($groupParts) > 1) {
            /*
             * UGLY WORKAROUND!!! @todo
             */
            // more than one group part...tricky!
            // @todo finde something better...
            $qb->resetDQLPart('groupBy');
            $qb->select('CONCAT('.implode(',', $groupParts).') as uniqueParts');

            $items = [];
            $result = $qb->getQuery()->getResult();
            foreach ($result as $row) {
                $items[] = $row['uniqueParts'];
            }
            $uniqueItems = array_unique($items);

            $this->rowCount = count($uniqueItems);
        } elseif (count($groupParts) == 1) {
            $groupPart = $groupParts[0];

            $qb->resetDQLPart('groupBy');
            $qb->select('COUNT(DISTINCT '.$groupPart.')');

            $this->rowCount = $qb->getQuery()->getSingleScalarResult();
        } else {
            // NO GROUP BY
            $countOneFunction = $qb->getEntityManager()
                ->getConfiguration()
                ->getCustomStringFunction('COUNT_ONE');
            if ($countOneFunction !== null) {
                $qb->select('COUNT_ONE() AS rowCount');
            } else {
                $fromPart = $dqlParts['from'];
                $qb->select('COUNT('.$fromPart[0]->getAlias().')');
            }

            $this->rowCount = $qb->getQuery()->getSingleScalarResult();
        }

        return $this->rowCount;
    }
1159604
protected function build()
    {
        $sql = "SELECT\n\t"
            . $this->columns . "\n"
            . $this->from . "\n"
            . ($this->join    ? $this->join           : null)
            . ($this->where   ? $this->where . "\n"   : null)
            . ($this->groupby ? $this->groupby . "\n" : null)
            . ($this->orderby ? $this->orderby . "\n" : null)
            . ($this->limit   ? $this->limit . "\n"   : null)
            . ($this->offset  ? $this->offset . "\n"  : null)
            . ";";

        return $sql;
    }
1068654
public function groupBy()
    {
        $args = func_get_args();
        while ($arg = array_shift($args)) {
            $this->group->add($arg);
        }
        return $this;
    }
259721
public function getGroup(): Group {
	
		if (!$this->issetCache('group')) {
			$this->setCache('group', new Group($this->groupId));
		}
	
		return $this->getCache('group');
	
	}
929963
public function buildQuery($query = null)
    {
        if (null === $query) {
            $query = $this->query;
        }

        $sql = "SELECT ";
        if (isset($query['distinct']) && $query['distinct']) {
            $sql .= 'DISTINCT ';
        }
        $sql .= isset($query['select']) ? $query['select'] : '*';

        if (!isset($query['from'])) {
            return false;
        }

        $sql .= "\nFROM " . $query['from'];
        if (isset($query['join'])) {
            $sql .= "\n" . (is_array($query['join']) ? implode("\n", $query['join']) : $query['join']);
        }
        if (isset($query['where']) && $query['where'] !== '') {
            $sql .= "\nWHERE " . $query['where'];
        }
        if (isset($query['group'])) {
            $sql .= "\nGROUP BY " . $query['group'];
            if (isset($query['having'])) {
                $sql .= "\nHAVING " . $query['having'];
            }
        }
        if (isset($query['order'])) {
            $sql .= "\n ORDER BY " . $query['order'];
        }
        $limit = isset($query['limit']) ? $query['limit'] : 0;
        $offset = isset($query['offset']) ? $query['offset'] : 0;
        $sql = $this->db->buildLimitOffset($sql, $limit, $offset);

        if (isset($query['union'])) {
            $sql .= "\n" . (is_array($query['union']) ? implode("\n", $query['union']) : $query['union']);
        }

        return $sql;
    }
728279
protected function buildHaving()
    {
        if (count($this->having) === 0) {
            return '';
        }

        // Use a ConditionSet to build the having clause
        $conditionSet = new Query\Condition\AndSet($this, $this->having);
        return "HAVING {$conditionSet->buildWithoutParentheses()}";
    }
154699
public function buildQuery()
    {
        $this->setSelectFrom();
        $this->setLeftJoins($this->qb);
        $this->setWhere($this->qb);
        $this->setWhereResultCallback($this->qb);
        $this->setWhereAllCallback($this->qb);
        $this->setOrderBy();
        $this->setLimit();

        return $this;
    }
1180283
protected function build()
    {
        $sql = $this->start . "\n"
            . ($this->from    ? $this->from . "\n"    : null)
            . ($this->join    ? $this->join           : null)
            . ($this->set     ? $this->set . "\n"     : null)
            . ($this->where   ? $this->where . "\n"   : null)
            . ($this->groupby ? $this->groupby . "\n" : null)
            . ($this->orderby ? $this->orderby . "\n" : null)
            . ($this->limit   ? $this->limit . "\n"   : null)
            . ($this->offset  ? $this->offset . "\n"  : null)
            . ";";

        return $sql;
    }
569364
public function get_sql() : string {
		if ( empty( $this->columns_to_select ) ) {
			// this should be the default behavior
			$this->columns_to_select = [ '*' ];
		}

		$from = ' FROM ' . implode( ', ', $this->from ) . ' ';

		if ( ! empty( $this->joins ) ) {
			$from .= $this->joins;
		}

		$from  .= 'WHERE ' . $this->where_clause;
		$select = 'SELECT ' . implode( ', ', $this->columns_to_select ) . $from;

		if ( ! empty( $this->group_by ) ) {
			$select .= 'GROUP BY ' . implode( ', ', $this->group_by ) . ' ';
		}

		if ( $this->order_by !== '' ) {
			$select .= 'ORDER BY ' . $this->order_by . ' ';
		}

		$select .= 'LIMIT ' . $this->limit;

		if ( $this->offset !== '' ) {
			$select .= ' OFFSET ' . $this->offset;
		}

		return $select;
	}
446785
public function getQuery()
    {
        $sql = $this->prepareSelectString();
        $sql .= $this->prepareJoinString();
        $sql .= $this->prepareWhereString();
        $sql .= $this->prepareGroupByString();
        $sql .= $this->prepareHavingString();
        $sql .= $this->prepareOrderByString();
        $sql .= $this->prepareLimitString();

        return $sql;
    }
266272
public function getGroupByCols()
    {
        if (!$this->containsAggregates()) {
            return [$this];
        }
        $cols = [];
        foreach ($this->getSourceExpressions() as $sourceExpression) {
            $cols[] = $sourceExpression->getGroupByCols();
        }

        return $cols;
    }
736548
public function GroupBy($Fields = NULL) {
      if(is_null($Fields)) {
         // Group by every item in the select that isn't a function.
         foreach($this->_Selects as $Alias => $Select) {
            if(ArrayValue('Function', $Select) == '') {
               $this->_GroupBys[] = $Select['Field'];
            }
         }
         return $this;
      }
      
      if (is_string($Fields))
         $Fields = explode(',', $Fields);

      foreach ($Fields as $Field) {
         $Field = trim($Field);

         if ($Field != '')
            $this->_GroupBys[] = $this->EscapeIdentifier($Field);
      }
      return $this;
   }
343631
public function GroupBy($fields): QueryBuilder
    {
        if (is_array($fields)) {
            $fields = implode(', ', $field);
        }
        
        $this->group_by = $fields;
        
        return $this;
    }
1224280
public function groupBy()
    {
        foreach (func_get_args() as $arg) {
            $this->groups = array_merge((array) $this->groups, is_array($arg) ? $arg : [$arg]);
        }

        return $this;
    }
531894
private function getSelectQuery()
    {
        return 'SELECT '.(empty($this->parts['select']) ? '*' : implode(', ', $this->parts['select'])).
               ' FROM '.$this->getFromClause().
               (($this->parts['where'] !== null) ? ' WHERE '.$this->parts['where'] : null).
               (!empty($this->parts['group_by']) ? ' GROUP BY '.implode(', ', $this->parts['group_by']) : null).
               (($this->parts['having'] !== null) ? ' HAVING '.$this->parts['having'] : null).
               (!empty($this->parts['order_by']) ? ' ORDER BY '.implode(', ', $this->parts['order_by']) : null).
               (($this->parts['limit'] !== null) ? ' LIMIT '.$this->parts['limit'] : null).
               (($this->parts['offset'] !== null) ? ' OFFSET '.$this->parts['offset'] : null);
    }