# `sample_variance`

## Description

Returns the sample variance of a numerical expression within a group.

- Useful for analyzing variability when working with incomplete datasets.
- Designed for scenarios like heavy trace sampling, where global variance would be misleading.

Note

Use `sample_variance` when only a subset of data is available. For complete datasets, prefer `variance`.

## Syntax

```dataprime
sample_variance(expression: number): number
```

## Arguments

| Name       | Type   | Required | Description                                            |
| ---------- | ------ | -------- | ------------------------------------------------------ |
| expression | number | **true** | Numerical expression whose sample variance is computed |

## Example

**Use case: Compute variance of trace durations**

Suppose you want to calculate how much the duration of each operation varies across traces, even when the dataset is incomplete.

### Example data

```json
{ "operationName": "cart", "duration": 50000 },
{ "operationName": "checkout", "duration": 200000 },
{ "operationName": "brokenPrinter", "duration": 700000 },
{ "operationName": "SendOrderConfirmation", "duration": 10200 },
{ "operationName": "cart", "duration": 300000 },
{ "operationName": "brokenPrinter", "duration": 1000000 },
{ "operationName": "checkout", "duration": 900000 },
{ "operationName": "cart", "duration": 600000 },
{ "operationName": "brokenPrinter", "duration": 400000 },
{ "operationName": "SendOrderConfirmation", "duration": 9950 },
{ "operationName": "checkout", "duration": 1200000 },
{ "operationName": "cart", "duration": 100000 },
{ "operationName": "brokenPrinter", "duration": 20000 },
{ "operationName": "SendOrderConfirmation", "duration": 10050 },
{ "operationName": "checkout", "duration": 1500000 },
{ "operationName": "cart", "duration": 800000 },
{ "operationName": "brokenPrinter", "duration": 800000 },
{ "operationName": "checkout", "duration": 500000 },
{ "operationName": "SendOrderConfirmation", "duration": 10100 },
{ "operationName": "cart", "duration": 400000 }
```

### Example query

```dataprime
source spans
| groupby $l.operationName aggregate sample_variance($m.duration) as duration_variance
```

### Example output

| operationName         | duration_variance  |
| --------------------- | ------------------ |
| brokenPrinter         | 56413398108.59468  |
| cart                  | 70558342573.80327  |
| checkout              | 111286897989.06136 |
| SendOrderConfirmation | 383221.0732801104  |
