# QBResult

## Overview

The `QBResult` class serves as a wrapper for managing query results in the `QBSelect` framework. It enables precise control over the structure and content of query results, including the addition of columns, aggregates, functions, case expressions, and subqueries. This flexibility allows developers to define custom outputs that align with specific SQL requirements.

Key features include support for distinct results, dynamic addition or removal of result components, and methods to include primary key columns automatically. Notable methods include `add(column, alias)` to add columns with aliases, `addSubSelect(query, alias)` for embedding subqueries, and `remove(name)` to remove a column by its name.

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

## Properties Summarized

| Type                                                                   | Name                  | Summary                                                                      |
| ---------------------------------------------------------------------- | --------------------- | ---------------------------------------------------------------------------- |
| [Boolean](/reference/servoycore/dev-api/js-lib/boolean.md)             | [distinct](#distinct) | Get/set the distinct flag for the query.                                     |
| [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                                                                         |
| ---------------------------------------------------------------------- | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------- |
| [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) | [add(column)](#add-column)                                                     | Add a column to the query result.                                               |
| [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) | [add(column, alias)](#add-column-alias)                                        | Add a column with alias to the query result.                                    |
| [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) | [add(columns)](#add-columns)                                                   | Add all columns from a query or a join to the query result.                     |
| [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) | [addPk()](#addpk)                                                              | Add the tables' primary pk columns in alphabetical order to the query result.   |
| [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) | [addSubSelect(query)](#addsubselect-query)                                     | Add a query to the query result.                                                |
| [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) | [addSubSelect(query, alias)](#addsubselect-query-alias)                        | Add a query with alias to the query result.                                     |
| [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) | [addSubSelect(customQuery, args)](#addsubselect-customquery-args)              | Add a custom subquery to the query result.                                      |
| [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) | [addSubSelect(customQuery, args, alias)](#addsubselect-customquery-args-alias) | Add a custom subquery with alias to the query result.                           |
| [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) | [addValue(value)](#addvalue-value)                                             | Add a value to the query result.                                                |
| [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) | [addValue(value, alias)](#addvalue-value-alias)                                | Add a value with an alias to the query result.                                  |
| [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) | [clear()](#clear)                                                              | Clear the columns in the query result.                                          |
| [Array](/reference/servoycore/dev-api/js-lib/array.md)                 | [getColumns()](#getcolumns)                                                    | returns an array with all the columns that will be in the select of this query. |
| [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) | [remove(column)](#remove-column)                                               | remove a column from the query result.                                          |
| [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) | [remove(name)](#remove-name)                                                   | Remove a column by name from the query result.                                  |

## Properties Detailed

### distinct

Get/set the distinct flag for the query.

**Type**\
[Boolean](/reference/servoycore/dev-api/js-lib/boolean.md) The current state of the distinct flag for the query.

**Sample**

```js
query.result.distinct = true
```

### 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 a column to the query result.

**Parameters**

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

**Returns:** [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) The query result object with the specified column added.

**Sample**

```js
query.result.add(query.columns.custname)
```

### add(column, alias)

Add a column with alias to the query result.

**Parameters**

* [QBColumn](/reference/servoycore/dev-api/database-manager/qbcolumn.md) **column** column to add to result
* [String](/reference/servoycore/dev-api/js-lib/string.md) **alias** column alias

**Returns:** [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) The query result object with the specified column and alias added.

**Sample**

```js
query.result.add(query.columns.custname, 'customer_name')
```

### add(columns)

Add all columns from a query or a join to the query result.

**Parameters**

* [QBColumns](/reference/servoycore/dev-api/database-manager/qbcolumns.md) **columns** columns to add to result

**Returns:** [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) The query result object with all columns from the specified query or join added.

**Sample**

```js
query.result.add(query.columns)
query.result.add(query.joins.orders_to_orderdetail.columns)
```

### addPk()

Add the tables' primary pk columns in alphabetical order to the query result.

**Returns:** [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) The query result object with primary key columns added.

**Sample**

```js
query.result.addPk()
```

### addSubSelect(query)

Add a query to the query result.

**Parameters**

* [QBSelect](/reference/servoycore/dev-api/database-manager/qbselect.md) **query** query to add to result

**Returns:** [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) The query result object with the specified query added.

**Sample**

```js
// make sure the query returns exactly 1 value.
query.result.addSubSelect(subquery);
```

### addSubSelect(query, alias)

Add a query with alias to the query result.

**Parameters**

* [QBSelect](/reference/servoycore/dev-api/database-manager/qbselect.md) **query** query to add to result
* [String](/reference/servoycore/dev-api/js-lib/string.md) **alias** result alias

**Returns:** [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) The query result object with the specified query and alias added.

**Sample**

```js
// make sure the query returns exactly 1 value.
query.result.addSubSelect(subquery, "mx");
```

### addSubSelect(customQuery, args)

Add a custom subquery to the query result.

**Parameters**

* [String](/reference/servoycore/dev-api/js-lib/string.md) **customQuery** query to add to result
* [Array](/reference/servoycore/dev-api/js-lib/array.md) **args** arguments to the query

**Returns:** [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) The query result object with the specified custom subquery added.

**Sample**

```js
// make sure the subquery returns exactly 1 value.
// select (select max from othertab where val = 'test') from tab
query.result.addSubSelect("select max(field) from othertab where val = ?", ["test"]);
```

### addSubSelect(customQuery, args, alias)

Add a custom subquery with alias to the query result.

**Parameters**

* [String](/reference/servoycore/dev-api/js-lib/string.md) **customQuery** query to add to result
* [Array](/reference/servoycore/dev-api/js-lib/array.md) **args** arguments to the query
* [String](/reference/servoycore/dev-api/js-lib/string.md) **alias** result alias

**Returns:** [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) The query result object with the specified custom subquery and alias added.

**Sample**

```js
// make sure the subquery returns exactly 1 value.
// select (select max from othertab where val = 'test') as mx from tab
query.result.addSubSelect("select max from othertab where val = ?", ["test"], "mx");
```

### addValue(value)

Add a value to the query result.

**Parameters**

* [Object](/reference/servoycore/dev-api/js-lib/object.md) **value** value add to result

**Returns:** [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) The query result object with the specified value added.

**Sample**

```js
query.result.addValue(100)
```

### addValue(value, alias)

Add a value with an alias to the query result.

**Parameters**

* [Object](/reference/servoycore/dev-api/js-lib/object.md) **value** value add to result
* [String](/reference/servoycore/dev-api/js-lib/string.md) **alias** value alias

**Returns:** [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) The query result object with the specified value and alias added.

**Sample**

```js
query.result.addValue(100, 'myvalue')
```

### clear()

Clear the columns in the query result.

**Returns:** [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) The query result object with all columns cleared.

**Sample**

```js
query.result.clear()
```

### getColumns()

returns an array with all the columns that will be in the select of this query.\
can return empty array. Then the system will auto append the pk when this query is used.

**Returns:** [Array](/reference/servoycore/dev-api/js-lib/array.md) An array of QBColumn thats in the select of this query.

**Sample**

```js
var columns = query.result.getColumns();
```

### remove(column)

remove a column from the query result.

**Parameters**

* [QBColumn](/reference/servoycore/dev-api/database-manager/qbcolumn.md) **column** column to remove from the result

**Returns:** [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) The query result object with the specified column removed.

**Sample**

```js
query.result.remove(query.columns.custname)
```

### remove(name)

Remove a column by name from the query result.

**Parameters**

* [String](/reference/servoycore/dev-api/js-lib/string.md) **name** name or alias of column to remove from the result

**Returns:** [QBResult](/reference/servoycore/dev-api/database-manager/qbresult.md) The query result object with the specified column removed by name or alias.

**Sample**

```js
query.result.remove("custname")
```

***


---

# 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/qbresult.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.
