Run an aggregate function (e.g. average, mode) on a table's field.
  • 01 Oct 2024
  • 3 Minutes to read
  • Contributors

Run an aggregate function (e.g. average, mode) on a table's field.


Article summary

Get
/tables/{tableId}/runAggregation

Requires tables:read API scope.

Attempting to run a numeric aggregate function e.g. sum on a non-numeric field type will generate a 422 error response. The only non-numeric functions are mode and uniqueValues.

Security
HTTP
Type basic

A Tulip API key is required to use the Tulip API. API key authentication uses HTTP Basic Authentication as defined by RFC 7617. HTTP Basic Authentication uses a generic username/password scheme to authenticate. For Tulip API requests, the password should be the API key's associated secret. The username should have the format {type}.{version}_{id}, where {type} is the API key type, {version} is the type's version number, and {id} is the id of the key. Tulip API keys currently are one of two types:

  • apikey.2 - user API keys provisioned by creating a Tulip API Token. The key id is the token id.
  • onetime.1 - temporary API keys provisioned using the /auth/temporary endpoint or using the One-Time API Key page. These keys are only valid for 30 seconds after provisioning.

Once you have determined the username and password you need to use, the Authorization header should be set to the value Basic {credentials}, where {credentials} is the base64-encoded value of the string {username}:{password}. See RFC 7617 for more details of this encoding.

Path parameters
tableId
stringRequired

The id of a Tulip Table.

Pattern^[a-zA-Z0-9_]+$
Exampleg56RCoZCtzv7borvp
Query parameters
fieldId
stringRequired

The field to aggregate on. An invalid field name for the specified table will result in a 404 error.

function
stringRequired

Aggregate function names are case-insensitive. An invalid function type will result in a 400 error.

Valid values[ "sum", "count", "avg", "min", "max", "mode", "uniqueValues" ]
limit
integerRequired

The maximum number of table records to evaluate.

Minimum1
Maximum100000
Example10
sortOptions
Array of object

Sorting allows to define which records are considered if there are more than the specified limit. Sort priority is determined by the order or options, i.e. sort by the first option, them by the second, etc. If ommitted, there is no guarantee as to which records are selected. A sortBy value that is not one of the table's field will generate a 422 error. Options for sortDir are "asc" and "desc".

Example: [{"sortBy": "superAwesomeField", "sortDir": "asc"}, {"sortBy": "lessAwesomeField", "sortDir": "desc"}]

object

Sorts are used to order the queries based on columns defined in the Tulip Table where the query exists.

Example{ "sortBy": "Color", "sortDir": "asc" }
sortBy
string Required

The field that records should be sorted by in the response. Can be the name of any of the columns of the Table (including id) or one of these special values:

  • _sequenceNumber
  • _createdAt
  • _updatedAt
sortDir
string Required

The direction of the records, either acsending or descending

Valid values[ "asc", "desc" ]
Default"asc"
filters
Array of object

An optional array of filter expressions to filter the Table records by. Each filter is an object specifying the field (name of a table column), functionType (comparison function), and arg (the value to compare to).

The field may be the name of a table column (including id) or one of these special values:

  • _createdAt
  • _updatedAt

The valid function types are:

  • equal
  • notEqual
  • blank
  • notBlank
  • greaterThanOrEqual
  • lessThanOrEqual
  • greaterThan
  • lessThan
  • contains
  • notContains
  • startsWith
  • notStartsWith
  • endsWith
  • notEndsWith
  • isIn
  • notIsIn

Note: The isIn and notIsIn functions take in array inputs. To pass an array input into a connector function you must convert the array to a string of this format: ["1", "2", "3"]. Also, the blank and notBlank functions do not take in any arguments at all.

Example: [{"field": "field1", "functionType": "equal", "arg": "1"},{"field": "field2", "functionType": "notBlank"}, {"field": "field3", "functionType": "isIn", "arg": ["1", "2", "3"]}]

object
field
string Required
functionType
string Required
Valid values[ "equal", "notEqual", "blank", "notBlank", "greaterThanOrEqual", "lessThanOrEqual", "greaterThan", "lessThan", "contains", "notContains", "startsWith", "notStartsWith", "endsWith", "notEndsWith", "isIn", "notIsIn" ]
arg
string
filterAggregator
string

How the filters in the filters parameter are combined.

  • all means that every filter must match a record in order for the record to be included.
  • any means at least one filter must match a record in order for the record to be included.
Valid values[ "all", "any" ]
Default"all"
Responses
200

The count of Tulip Table records was retrieved successfully.

object
result
number
400

The request was malformed. This could mean that headers, query parameters, or the request body was unable to be parsed or had unexpected values.

object
errorCode
string
errorUniqueID
string
Pattern^[a-zA-Z0-9+/]+$
Exampleaq21mSKC1rbO87TjC/4Hz2EJHd/v+jxf7MtC315vo0Y
details
string
401

The request was made unauthorized. HTTP Basic Authorization using a Tulip API Key is required for use of the API.

object
errorCode
string
errorUniqueID
string
Pattern^[a-zA-Z0-9+/]+$
Exampleaq21mSKC1rbO87TjC/4Hz2EJHd/v+jxf7MtC315vo0Y
details
string
errorCode
Valid values[ "AuthenticationRequired" ]
403

The provided authentication info was rejected. The response will provide additional details.

object
errorCode
string
errorUniqueID
string
Pattern^[a-zA-Z0-9+/]+$
Exampleaq21mSKC1rbO87TjC/4Hz2EJHd/v+jxf7MtC315vo0Y
details
string
errorCode
Valid values[ "AuthenticationFailed" ]
404

The requested database entry was not found.

object
errorCode
string
errorUniqueID
string
Pattern^[a-zA-Z0-9+/]+$
Exampleaq21mSKC1rbO87TjC/4Hz2EJHd/v+jxf7MtC315vo0Y
details
string
errorCode
Valid values[ "NoSuchDatabaseEntry" ]
422

The request was syntactically sound, but could not be processed due to a logical problem.

object
errorCode
string
errorUniqueID
string
Pattern^[a-zA-Z0-9+/]+$
Exampleaq21mSKC1rbO87TjC/4Hz2EJHd/v+jxf7MtC315vo0Y
details
string
errorCode
Valid values[ "UnprocessableEntity" ]
500

The server encountered an unexpected error.

object
errorCode
string
errorUniqueID
string
Pattern^[a-zA-Z0-9+/]+$
Exampleaq21mSKC1rbO87TjC/4Hz2EJHd/v+jxf7MtC315vo0Y
details
string

Was this article helpful?