---
title: measure aggregate function
description: Learn the syntax of the measure expression of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/functions/measure
---

# `measure` aggregate function

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 16.4 and above

Returns the `measure_column` aggregated from the values of a group. In Databricks Runtime 18.1 and above, [`agg` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/agg.md) is a synonym for this function.

Unlike a regular aggregate function like `SUM`, `AVG`, or `COUNT`, the `MEASURE` function does not specify the aggregation.
It inherits the definition of the aggregation from the [metric view definition](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-create-view.md).

Using a metric view with measures is superior to regular views because it abstracts the complexity of the underlying aggregations while giving the invoker the freedom to choose the grouping columns.

## Syntax

```
measure ( measure_column ) [ FILTER ( WHERE cond ) ]
```

This function cannot be invoked as a [window function](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md) using the `OVER` clause.

## Arguments

* [measure\_column](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-names.md#column-name): A reference to a measure column in a metric view.
* `cond`: An optional boolean expression in a [FILTER clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-groupby.md) that filters the rows used to compute the measure.

  **Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 18.1 and above

## Returns

A value of the type of `measure_column`.

## Aggregation across joins

Query-time joins are in [Beta](https://docs.databricks.com/aws/en/release-notes/release-types.md).

Query aggregates such as `SUM(field)` and `COUNT(*)` include rows repeated by the join and can double-count. `MEASURE()` preserves the source metric view's grain, so join repetitions do not inflate measures.

For unmatched outer-join groups, measures defined as `COUNT(*)` return `0`, while measures defined as `SUM(...)` return `NULL`.

For a complete worked example, see [How measures are computed across joins](https://docs.databricks.com/aws/en/uc-semantics/metric-views/user-joins.md#computation).

## FILTER clause behavior

When you apply a `FILTER` clause to a measure, the filter condition is applied to every aggregate function in the measure's definition:

* If an aggregate function in the definition has no `FILTER` clause, the measure's condition is applied to it.
* If an aggregate function in the definition already has a `FILTER` clause, the measure's condition is combined with the existing one using `AND`.

The same rules apply recursively when a measure references another measure.

For window measures, the `FILTER` clause is applied *after* the window aggregation, equivalent to placing the same condition in the query's `WHERE` clause.

In Databricks Runtime versions below 18.1, a `FILTER` clause on a measure returns an error.

## Examples

```sql
-- A metric view with a measure column 4 metric columns
CREATE OR REPLACE VIEW region_sales_metrics
  (month COMMENT 'Month order was made',
   status,
   order_priority,
   count_orders COMMENT 'Count of orders',
   total_Revenue,
   total_Revenue_p_Customer,
   total_revenue_for_open_orders)
  WITH METRICS
  LANGUAGE YAML
  COMMENT 'A metric view for regional sales metrics.'
  AS $$
   version: 0.1
   source: samples.tpch.orders
   filter: o_orderdate > '1990-01-01'
   dimensions:
   - name: month
     expr: date_trunc('MONTH', o_orderdate)
   - name: status
     expr: case
       when o_orderstatus = 'O' then 'Open'
       when o_orderstatus = 'P' then 'Processing'
       when o_orderstatus = 'F' then 'Fulfilled'
       end
   - name: order_priority
     expr: split(o_orderpriority, '-')[1]
   measures:
   - name: count_orders
     expr: count(1)
   - name: total_revenue
     expr: SUM(o_totalprice)
   - name: total_revenue_per_customer
     expr: SUM(o_totalprice) / count(distinct o_custkey)
   - name: total_revenue_for_open_orders
     expr: SUM(o_totalprice) filter (where o_orderstatus='O')
  $$;

-- Tracking total_revenue_per_customer by month in 1995
> SELECT extract(month from month) as month,
    measure(total_revenue_per_customer)::bigint AS total_revenue_per_customer
  FROM region_sales_metrics
  WHERE extract(year FROM month) = 1995
  GROUP BY ALL
  ORDER BY ALL;
  month	 total_revenue_per_customer
  -----  --------------------------
   1     167727
   2     166237
   3     167349
   4     167604
   5     166483
   6     167402
   7     167272
   8     167435
   9     166633
  10     167441
  11     167286
  12     167542

-- Tracking total_revenue_per_customer by month and status in 1995
> SELECT extract(month from month) as month,
    status,
    measure(total_revenue_per_customer)::bigint AS total_revenue_per_customer
  FROM region_sales_metrics
  WHERE extract(year FROM month) = 1995
  GROUP BY ALL
  ORDER BY ALL;
  month  status      total_revenue_per_customer
  -----  ---------   --------------------------
   1     Fulfilled   167727
   2     Fulfilled   161720
   2    Open          40203
   2    Processing   193412
   3    Fulfilled    121816
   3    Open          52424
   3    Processing   196304
   4    Fulfilled     80405
   4    Open          75630
   4    Processing   196136
   5    Fulfilled     53460
   5    Open         115344
   5    Processing   196147
   6    Fulfilled     42479
   6    Open         160390
   6    Processing   193461
   7    Open         167272
   8    Open         167435
   9    Open         166633
   10   Open         167441
   11   Open         167286
   12   Open         167542

-- Compare total revenue to revenue from fulfilled orders by month in 1995.
-- The FILTER condition is pushed down to the SUM aggregate in the total_revenue definition.
> SELECT extract(month from month) as month,
    measure(total_revenue)::bigint AS total_revenue,
    measure(total_revenue) FILTER (WHERE status = 'Fulfilled') AS fulfilled_revenue
  FROM region_sales_metrics
  WHERE extract(year FROM month) = 1995
  GROUP BY ALL
  ORDER BY ALL;
```

## Related

* [Join metric views in SQL](https://docs.databricks.com/aws/en/uc-semantics/metric-views/user-joins.md)
* [`agg` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/agg.md)
* [`aggregate` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/aggregate.md)
* [`avg` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/avg.md)
* [CREATE VIEW](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-create-view.md)
* [`listagg` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/listagg.md)
* [`max` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/max.md)
* [`mean` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/mean.md)
* [`min` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/min.md)
* [`string_agg` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/string_agg.md)
* [`try_sum` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_sum.md)
* [`try_avg` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_avg.md)
