Creating an Aggregated Query
Aggregated queries provide a summarized view of a dataset by applying an aggregate function like sum, avg, min, max
over a group of one or many fields.
The simplest way of creating an aggregated query involves defining a query configuration object with the fields you want to group-by
and the metrics
you want to request.
You also need to provide an object with the Zoomdata data source name when calling the createQuery
method of the client
instance.
Example:
// create the query configuration object
const queryConfig = {
groups: [ { name: 'city', limit: 10, sort: { name: 'city', dir: 'asc', }, }, ], metrics: [{ name: 'price', func: 'avg' }],};
// create an object with the Zoomdata source name to use when running the query.
// In our example, the source name = 'Sales'
const source = {
name: 'Sales',
};
/**
* Used to create a data query.
*
* Result of running this function can be later used as input for client.runQuery.
* @param {SourceObject} source Object with a single key, name, that gives the name of the data source being queried, or data source model.
* @param {QueryConfig} config Query options.
* @returns {Promise} Promise with QueryAPI object. It can be used as input for client.runQuery method
* @example
*/
client.createQuery(source, queryConfig);
The createQuery
method returns a JavaScript Promise that resolves with a queryAPI
object.
In the above example, the query requests the average price
grouped by city
.
We limit the results to 10
cities based on the sorting of city
in ascending
order.
Try this example on CodeSandbox
It should output the queryAPI
object to the console.
Updated Jan 10, 2019