Aggregate Table

https://{your-workspace-slug}.{region}.xata.sh/db/db_branch_name/tables/table_name/aggregate

Expected Parameters

NameDescriptionInRequiredSchema
db_branch_nameThe DBBranchName matches the pattern `{db_name}:{branch_name}`. pathstring
table_nameThe Table namepathstring

Run Aggregations Over a Table

POST
https://{your-workspace-slug}.{region}.xata.sh/db/db_branch_name/tables/table_name/aggregate

This endpoint allows you to run aggregations (analytics) on the data from one table. While the summary endpoint is served from a transactional store and the results are strongly consistent, the aggregate endpoint is served from our columnar store and the results are only eventually consistent. On the other hand, the aggregate endpoint uses a store that is more appropiate for analytics, makes use of approximative algorithms (e.g for cardinality), and is generally faster and can do more complex aggregations.

For usage, see the API Guide.

Request Body Type Definition

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
type AggregateTable = {
    filter?: FilterExpression;
    aggs?: AggExpressionMap;
};

/**
 * @minProperties 1
 */
type FilterExpression = {
    $exists?: string;
    $existsNot?: string;
    $any?: FilterList;
    $all?: FilterList;
    $none?: FilterList;
    $not?: FilterList;
} & {
    [key: string]: FilterColumn;
};

/**
 * The description of the aggregations you wish to receive.
 * 
 * @example {"totalCount":{"count":"*"},"dailyActiveUsers":{"dateHistogram":{"column":"date","interval":"1d","aggs":{"uniqueUsers":{"uniqueCount":{"column":"userID"}}}}}}
 */
type AggExpressionMap = {
    [key: string]: AggExpression;
};

type FilterList = FilterExpression | FilterExpression[];

type FilterColumn = FilterColumnIncludes | FilterPredicate | FilterList;

/**
 * The description of a single aggregation operation. It is an object with only one key-value pair.
 * The key represents the aggregation type, while the value is an object with the configuration of
 * the aggregation.
 */
type AggExpression = {
    count?: CountAgg;
} | {
    sum?: SumAgg;
} | {
    max?: MaxAgg;
} | {
    min?: MinAgg;
} | {
    average?: AverageAgg;
} | {
    uniqueCount?: UniqueCountAgg;
} | {
    dateHistogram?: DateHistogramAgg;
} | {
    topValues?: TopValuesAgg;
} | {
    numericHistogram?: NumericHistogramAgg;
};

/**
 * @maxProperties 1
 * @minProperties 1
 */
type FilterColumnIncludes = {
    $includes?: FilterPredicate;
    $includesAny?: FilterPredicate;
    $includesAll?: FilterPredicate;
    $includesNone?: FilterPredicate;
};

type FilterPredicate = FilterValue | FilterPredicate[] | FilterPredicateOp | FilterPredicateRangeOp;

/**
 * Count the number of records with an optional filter.
 */
type CountAgg = {
    filter?: FilterExpression;
} | ("*");

/**
 * The sum of the numeric values in a particular column.
 */
type SumAgg = {
    /*
     * The column on which to compute the sum. Must be a numeric type.
     */
    column: string;
};

/**
 * The max of the numeric values in a particular column.
 */
type MaxAgg = {
    /*
     * The column on which to compute the max. Must be a numeric type.
     */
    column: string;
};

/**
 * The min of the numeric values in a particular column.
 */
type MinAgg = {
    /*
     * The column on which to compute the min. Must be a numeric type.
     */
    column: string;
};

/**
 * The average of the numeric values in a particular column.
 */
type AverageAgg = {
    /*
     * The column on which to compute the average. Must be a numeric type.
     */
    column: string;
};

/**
 * Count the number of distinct values in a particular column.
 */
type UniqueCountAgg = {
    /*
     * The column from where to count the unique values.
     */
    column: string;
    /*
     * The threshold under which the unique count is exact. If the number of unique
     * values in the column is higher than this threshold, the results are approximate.
     * Maximum value is 40,000, default value is 3000.
     */
    precisionThreshold?: number;
};

/**
 * Split data into buckets by a datetime column. Accepts sub-aggregations for each bucket.
 */
type DateHistogramAgg = {
    /*
     * The column to use for bucketing. Must be of type datetime.
     */
    column: string;
    /*
     * The fixed interval to use when bucketing.
     * It is formatted as number + units, for example: `5d`, `20m`, `10s`.
     *
     * @pattern ^(\d+)(d|h|m|s|ms)$
     */
    interval?: string;
    /*
     * The calendar-aware interval to use when bucketing. Possible values are: `minute`,
     * `hour`, `day`, `week`, `month`, `quarter`, `year`.
     */
    calendarInterval?: "minute" | "hour" | "day" | "week" | "month" | "quarter" | "year";
    /*
     * The timezone to use for bucketing. By default, UTC is assumed.
     * The accepted format is as an ISO 8601 UTC offset. For example: `+01:00` or
     * `-08:00`.
     *
     * @pattern ^[+-][01]\d:[0-5]\d$
     */
    timezone?: string;
    aggs?: AggExpressionMap;
};

/**
 * Split data into buckets by the unique values in a column. Accepts sub-aggregations for each bucket.
 * The top values as ordered by the number of records (`$count`) are returned.
 */
type TopValuesAgg = {
    /*
     * The column to use for bucketing. Accepted types are `string`, `email`, `int`, `float`, or `bool`.
     */
    column: string;
    aggs?: AggExpressionMap;
    /*
     * The maximum number of unique values to return.
     *
     * @default 10
     * @maximum 1000
     */
    size?: number;
};

/**
 * Split data into buckets by dynamic numeric ranges. Accepts sub-aggregations for each bucket.
 */
type NumericHistogramAgg = {
    /*
     * The column to use for bucketing. Must be of numeric type.
     */
    column: string;
    /*
     * The numeric interval to use for bucketing. The resulting buckets will be ranges
     * with this value as size.
     *
     * @minimum 0
     */
    interval: number;
    /*
     * By default the bucket keys start with 0 and then continue in `interval` steps. The bucket
     * boundaries can be shifted by using the offset option. For example, if the `interval` is 100,
     * but you prefer the bucket boundaries to be `[50, 150), [150, 250), etc.`, you can set `offset`
     * to 50.
     *
     * @default 0
     */
    offset?: number;
    aggs?: AggExpressionMap;
};

type FilterValue = number | string | boolean;

/**
 * @maxProperties 1
 * @minProperties 1
 */
type FilterPredicateOp = {
    $any?: FilterPredicate[];
    $all?: FilterPredicate[];
    $none?: FilterPredicate | FilterPredicate[];
    $not?: FilterPredicate | FilterPredicate[];
    $is?: FilterValue | FilterValue[];
    $isNot?: FilterValue | FilterValue[];
    $lt?: FilterRangeValue;
    $le?: FilterRangeValue;
    $gt?: FilterRangeValue;
    $ge?: FilterRangeValue;
    $contains?: string;
    $startsWith?: string;
    $endsWith?: string;
    $pattern?: string;
};

/**
 * @maxProperties 2
 * @minProperties 2
 */
type FilterPredicateRangeOp = {
    $lt?: FilterRangeValue;
    $le?: FilterRangeValue;
    $gt?: FilterRangeValue;
    $ge?: FilterRangeValue;
};

type FilterRangeValue = number | string;
Status CodeDescriptionExample Response/Type Definition
200OK
{
  "aggs": {
    "dailyUniqueUsers": {
      "values": [
        {
          "$count": 321,
          "$key": "2022-02-22T22:22:22Z",
          "uniqueUsers": 134
        },
        {
          "$count": 202,
          "$key": "2022-02-23T22:22:22Z",
          "uniqueUsers": 90
        }
      ]
    }
  }
}
400Bad Request
type AggregateTable = {
    id?: string;
    message: string;
};
401Authentication Error
{
  "message": "invalid API key"
}
404Example response
type AggregateTable = {
    id?: string;
    message: string;
};
5XXUnexpected Error