# QBGroupBy

## Overview

The `QBGroupBy` class is utilized to define SQL `GROUP BY` conditions within a `QBSelect` query. It allows adding columns, functions, or primary key columns to the `GROUP BY` clause. This class also provides the capability to clear the existing `GROUP BY` clause, ensuring flexibility in query construction.

**Key Features**

The `parent` property links the `QBGroupBy` instance to its parent table clause, which can be a query or a join clause. The `root` property provides access to the parent query builder, enabling hierarchical query manipulation.

The `add(column)` and `add(function)` methods enable adding columns or functions to the `GROUP BY` clause, supporting aggregation queries. The `addPk()` method simplifies grouping by adding primary key columns in alphabetical order. Additionally, the `clear()` method removes all conditions from the `GROUP BY` clause, allowing for dynamic query adjustments.

For further details on query construction and execution, refer to the [QBSelect](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/qbselect) section of the documentation.

## Properties Summarized

| Type                                                                   | Name              | Summary                                                                      |
| ---------------------------------------------------------------------- | ----------------- | ---------------------------------------------------------------------------- |
| [QBSelect](/reference/servoycore/dev-api/database-manager/qbselect.md) | [parent](#parent) | Get query builder parent table clause, this may be a query or a join clause. |
| [QBSelect](/reference/servoycore/dev-api/database-manager/qbselect.md) | [root](#root)     | Get query builder parent.                                                    |

## Methods Summarized

| Type                                                                     | Name                       | Summary                                                                          |
| ------------------------------------------------------------------------ | -------------------------- | -------------------------------------------------------------------------------- |
| [QBGroupBy](/reference/servoycore/dev-api/database-manager/qbgroupby.md) | [add(column)](#add-column) | Add column name to group-by clause.                                              |
| [QBGroupBy](/reference/servoycore/dev-api/database-manager/qbgroupby.md) | [addPk()](#addpk)          | Add the tables' primary pk columns in alphabetical order to the group by clause. |
| [QBGroupBy](/reference/servoycore/dev-api/database-manager/qbgroupby.md) | [clear()](#clear)          | Clear the to group-by clause.                                                    |

## Properties Detailed

### parent

Get query builder parent table clause, this may be a query or a join clause.

**Type**\
[QBSelect](/reference/servoycore/dev-api/database-manager/qbselect.md)

**Sample**

```js
var query = datasources.db.example_data.person.createSelect();
	query.where.add(query.joins.person_to_parent.joins.person_to_parent.columns.name.eq('john'))
	foundset.loadRecords(query)
```

### root

Get query builder parent.

**Type**\
[QBSelect](/reference/servoycore/dev-api/database-manager/qbselect.md)

**Sample**

```js
var subquery = datasources.db.example_data.order_details.createSelect();

	var query = datasources.db.example_data.orders.createSelect();
	query.where.add(query
		.or
			.add(query.columns.order_id.not.isin([1, 2, 3]))

			.add(query.exists(
					subquery.where.add(subquery.columns.orderid.eq(query.columns.order_id)).root
			))
		)

	foundset.loadRecords(query)
```

## Methods Detailed

### add(column)

Add column name to group-by clause.

Same as query.groupBy().add(join.getColumn("value"))

**Parameters**

* [QBColumn](/reference/servoycore/dev-api/database-manager/qbcolumn.md) **column** the column to add to the query condition

**Returns:** [QBGroupBy](/reference/servoycore/dev-api/database-manager/qbgroupby.md) the updated group-by clause with the specified column added.

**Sample**

```js
var query = datasources.db.example_data.orders.createSelect();
query.groupBy.add(query.columns.orderid) // have to group by on pk when using having-conditions in (foundset) pk queries
.root.having.add(query.joins.orders_to_order_details.columns.quantity.count.eq(0))
foundset.loadRecords(query)
```

### addPk()

Add the tables' primary pk columns in alphabetical order to the group by clause.

**Returns:** [QBGroupBy](/reference/servoycore/dev-api/database-manager/qbgroupby.md) the updated group-by clause with the primary key(s) of the table added.

**Sample**

```js
var query = datasources.db.example_data.orders.createSelect();
query.groupBy.addPk() // have to group by on pk when using having-conditions in (foundset) pk queries
.root.having.add(query.joins.orders_to_order_details.columns.quantity.count.eq(0))
foundset.loadRecords(query)
```

### clear()

Clear the to group-by clause.

**Returns:** [QBGroupBy](/reference/servoycore/dev-api/database-manager/qbgroupby.md) the cleared group-by clause.

**Sample**

```js
var q = foundset.getQuery()
q.where.add(q.columns.x.eq(100))
query.groupBy.clear.root.clearHaving()
foundset.loadRecords(q);
```

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.servoy.com/reference/servoycore/dev-api/database-manager/qbgroupby.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
