QBAggregates

Overview

The QBAggregates class provides a collection of aggregate functions that can be utilized within a QBSelect query. These functions enable the creation of expressions for common operations such as averages, counts, maximums, minimums, and sums. They are designed to work within query results or as part of grouping and filtering logic.

Key methods include avg, count, max, min, and sum, which allow you to compute aggregate values for specific columns or expressions. Additionally, the parent and root properties give access to the parent query or table clause, facilitating complex query structures and subqueries.

For more information about constructing and executing queries, refer to QBSelect section of the documentation.

Properties Summarized

Type
Name
Summary

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

Get query builder parent.

Methods Summarized

Type
Name
Summary

Create avg(value) expression

Create count(*) expression

Create count(value) expression

Create max(value) expression

Create min(value) expression

Create sum(value) expression

Properties Detailed

parent

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

Type QBSelect

Sample

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

Sample

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

avg({Object})

Create avg(value) expression

Parameters

  • Object {Object} aggregee - The column or expression to calculate the average for.

Returns: QBColumn A QBAggregate object representing the average operation for the specified aggregee.

Sample

var query = datasources.db.example_data.orders.createSelect();
	query.result.add(query.aggregates.avg(query.columns.amount)).add(query.columns.countryCode)
	query.groupBy.add(query.columns.countryCode)
 var ds = databaseManager.getDataSetByQuery(query, 100);

count()

Create count(*) expression

Returns: QBColumn A QBAggregate object representing the count operation.

Sample

var query = datasources.db.example_data.orders.createSelect();
	query.result.add(query.aggregates.count().add(query.columns.countryCode)
	query.groupBy.add(query.columns.countryCode)
 var ds = databaseManager.getDataSetByQuery(query, 100);

count({Object})

Create count(value) expression

Parameters

  • Object {Object} aggregee - The column, expression, or value to count. Can also be a special value like "*" for counting all rows.

Returns: QBColumn A QBAggregate object representing the count operation with the specified aggregee.

Sample

var query = datasources.db.example_data.orders.createSelect();
	query.result.add(query.aggregates.count(query.columns.amount)).add(query.columns.countryCode)
	query.groupBy.add(query.columns.countryCode)
 var ds = databaseManager.getDataSetByQuery(query, 100);

max({Object})

Create max(value) expression

Parameters

  • Object {Object} aggregee - The column or expression to calculate the maximum value for. This can be a specific column or a computed expression.

Returns: QBColumn A QBAggregate object representing the maximum value operation for the specified aggregee.

Sample

var query = datasources.db.example_data.orders.createSelect();
	query.result.add(query.aggregates.max(query.columns.amount)).add(query.columns.countryCode)
	query.groupBy.add(query.columns.countryCode)
 var ds = databaseManager.getDataSetByQuery(query, 100);

min({Object})

Create min(value) expression

Parameters

  • Object {Object} aggregee - The column or expression to calculate the minimum value for. This can be a specific column or a computed expression.

Returns: QBColumn A QBAggregate object representing the minimum value operation for the specified aggregee.

Sample

var query = datasources.db.example_data.orders.createSelect();
	query.result.add(query.aggregates.min(query.columns.amount)).add(query.columns.countryCode)
	query.groupBy.add(query.columns.countryCode)
 var ds = databaseManager.getDataSetByQuery(query, 100);

sum({Object})

Create sum(value) expression

Parameters

  • Object {Object} aggregee - The column or expression to calculate the sum for. This can be a specific column or a computed expression.

Returns: QBColumn A QBAggregate object representing the sum operation for the specified aggregee.

Sample

var query = datasources.db.example_data.orders.createSelect();
	query.result.add(query.aggregates.sum(query.columns.amount)).add(query.columns.countryCode)
	query.groupBy.add(query.columns.countryCode)
 var ds = databaseManager.getDataSetByQuery(query, 100);

Last updated