|
1 | | ---- |
2 | | -redirect_from: |
3 | | - - /types-and-formats |
4 | | - - /schema/reference/types-and-formats |
5 | | ---- |
6 | | - |
7 | 1 | # Types and Formats |
8 | 2 |
|
9 | 3 | ## Measure Types |
@@ -139,7 +133,7 @@ cubes: |
139 | 133 |
|
140 | 134 | ### `number` |
141 | 135 |
|
142 | | -Type `number` is usually used, when performing |
| 136 | +The `number` type is usually used, when performing arithmetic operations on |
143 | 137 | arithmetic operations on measures. [Learn more about Calculated |
144 | 138 | Measures][ref-schema-ref-calc-measures]. |
145 | 139 |
|
@@ -207,10 +201,59 @@ cubes: |
207 | 201 |
|
208 | 202 | </CodeTabs> |
209 | 203 |
|
| 204 | +### `number_agg` |
| 205 | + |
| 206 | +The `number_agg` type is used when you need to write a custom aggregate function |
| 207 | +in the `sql` parameter that isn't covered by standard measure types like `sum`, |
| 208 | +`avg`, `min`, etc. |
| 209 | + |
| 210 | +<WarningBox> |
| 211 | + |
| 212 | +The `number_agg` type is only available in Tesseract, the [next-generation data modeling |
| 213 | +engine][link-tesseract]. Tesseract is currently in preview. Use the |
| 214 | +`CUBEJS_TESSERACT_SQL_PLANNER` environment variable to enable it. |
| 215 | + |
| 216 | +</WarningBox> |
| 217 | + |
| 218 | +Unlike the `number` type which is used for calculations on measures (e.g., |
| 219 | +`SUM(revenue) / COUNT(*)`), `number_agg` indicates that the `sql` parameter contains |
| 220 | +a direct SQL aggregate function. |
| 221 | + |
| 222 | +The `sql` parameter is required and must include a custom aggregate function that returns a numeric |
| 223 | +value. |
| 224 | + |
| 225 | +<CodeTabs> |
| 226 | + |
| 227 | +```javascript |
| 228 | +cube(`orders`, { |
| 229 | + // ... |
| 230 | + |
| 231 | + measures: { |
| 232 | + median_price: { |
| 233 | + sql: `PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY price)`, |
| 234 | + type: `number_agg` |
| 235 | + } |
| 236 | + } |
| 237 | +}) |
| 238 | +``` |
| 239 | + |
| 240 | +```yaml |
| 241 | +cubes: |
| 242 | + - name: orders |
| 243 | + # ... |
| 244 | + |
| 245 | + measures: |
| 246 | + - name: median_price |
| 247 | + sql: "PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY price)" |
| 248 | + type: number_agg |
| 249 | +``` |
| 250 | +
|
| 251 | +</CodeTabs> |
| 252 | +
|
210 | 253 | ### `count` |
211 | 254 |
|
212 | | -Performs a table count, similar to SQL’s `COUNT` function. However, unlike |
213 | | -writing raw SQL, Cube will properly calculate counts even if your query’s |
| 255 | +Performs a table count, similar to SQL's `COUNT` function. However, unlike |
| 256 | +writing raw SQL, Cube will properly calculate counts even if your query's |
214 | 257 | joins will produce row multiplication. |
215 | 258 |
|
216 | 259 | You do not need to include a `sql` parameter for this type. |
@@ -254,7 +297,7 @@ cubes: |
254 | 297 |
|
255 | 298 | ### `count_distinct` |
256 | 299 |
|
257 | | -Calculates the number of distinct values in a given field. It makes use of SQL’s |
| 300 | +Calculates the number of distinct values in a given field. It makes use of SQL's |
258 | 301 | `COUNT DISTINCT` function. |
259 | 302 |
|
260 | 303 | The `sql` parameter is required and must include any valid SQL expression |
@@ -332,9 +375,9 @@ cubes: |
332 | 375 |
|
333 | 376 | ### `sum` |
334 | 377 |
|
335 | | -Adds up the values in a given field. It is similar to SQL’s `SUM` function. |
| 378 | +Adds up the values in a given field. It is similar to SQL's `SUM` function. |
336 | 379 | However, unlike writing raw SQL, Cube will properly calculate sums even if your |
337 | | -query’s joins will result in row duplication. |
| 380 | +query's joins will result in row duplication. |
338 | 381 |
|
339 | 382 | The `sql` parameter is required and must include any valid SQL expression |
340 | 383 | of the numeric type (without an aggregate function). |
@@ -387,9 +430,9 @@ cubes: |
387 | 430 |
|
388 | 431 | ### `avg` |
389 | 432 |
|
390 | | -Averages the values in a given field. It is similar to SQL’s AVG function. |
| 433 | +Averages the values in a given field. It is similar to SQL's AVG function. |
391 | 434 | However, unlike writing raw SQL, Cube will properly calculate averages even if |
392 | | -your query’s joins will result in row duplication. |
| 435 | +your query's joins will result in row duplication. |
393 | 436 |
|
394 | 437 | The `sql` parameter is required and must include any valid SQL expression |
395 | 438 | of the numeric type (without an aggregate function). |
@@ -494,7 +537,7 @@ cubes: |
494 | 537 |
|
495 | 538 | ## Measure Formats |
496 | 539 |
|
497 | | -When creating a **measure** you can explicitly define the format you’d like to |
| 540 | +When creating a **measure** you can explicitly define the format you'd like to |
498 | 541 | see as output. |
499 | 542 |
|
500 | 543 | ### `percent` |
|
0 commit comments