Query metric views
You can query metric views like standard views from any SQL editor attached to a SQL warehouse or compute resource running a supported runtime. Metric views enable flexible grouping and filtering. You can analyze measures across any combination of fields (also called dimensions) at runtime without pre-computing every aggregation. The queries on this page demonstrate common query patterns.
Query measures and fields
All measure evaluations in a metric view query must use the MEASURE aggregate function. For complete details and syntax, see measure aggregate function. In Databricks Runtime 18.1 and above, you can also use agg.
When you query a metric view, SELECT * isn't supported. To select all fields and measures in a query, explicitly list each field and wrap each measure with the MEASURE() aggregate function. Measures require the MEASURE() function to evaluate properly.
This applies only to queries. When you define a metric view, you can use wildcards to import all fields and measures from a source. See Bulk-import fields and measures with wildcards.
Query with grouping
SELECT
`Order Month`,
`Order Status`,
MEASURE(`Order Count`),
MEASURE(`Total Revenue`)
FROM orders_metric_view
GROUP BY ALL
ORDER BY `Order Month`;
Query with filtering
SELECT
o_orderpriority,
MEASURE(`Total Revenue`),
MEASURE(`Total Revenue per Customer`)
FROM orders_metric_view
WHERE `Order Status` = 'Fulfilled'
GROUP BY o_orderpriority;
Query with other tables
Query-time joins (Beta) let you join a metric view directly with another metric view, table, or view. For compute requirements, see Requirements. Join on fields and wrap each measure in MEASURE(). The following query groups order measures by a customer attribute from the joined table. The metric view must expose o_custkey as a field:
SELECT
c.c_mktsegment,
MEASURE(o.`Order Count`) AS order_count,
MEASURE(o.`Total Revenue`) AS total_revenue
FROM orders_metric_view o
JOIN samples.tpch.customer c ON o.o_custkey = c.c_custkey
GROUP BY c.c_mktsegment
ORDER BY c.c_mktsegment;
For supported join types, examples, and restrictions, see Join metric views in SQL.
To join results that you have already aggregated, wrap the metric view query in a common table expression (CTE). The aggregated values become regular columns:
WITH orders AS (
SELECT
o_custkey,
MEASURE(`Order Count`) AS order_count,
MEASURE(`Total Revenue`) AS total_revenue
FROM orders_metric_view
GROUP BY o_custkey
)
SELECT
c.c_mktsegment,
orders.order_count,
orders.total_revenue
FROM orders
JOIN samples.tpch.customer c ON orders.o_custkey = c.c_custkey
ORDER BY c.c_mktsegment;
Query a metric view with parameters
To query a metric view that defines parameters, call it as a table-valued function and pass each parameter as a named argument. For details and examples, see Query a metric view with parameters.
View metric view definition and metadata
The following query returns the full YAML definition for a metric view, including measures, fields, joins, and agent metadata. The AS JSON parameter is optional. For complete syntax details, see JSON formatted output.
DESCRIBE TABLE EXTENDED <catalog.schema.metric_view_name> AS JSON
The complete YAML definition is shown in the View Text field in the results. Each column contains a metadata field that holds agent metadata.
Consume metric views in tools
You can also use metric views throughout the Databricks workspace and in external tools:
Tool | Description |
|---|---|
Use metric views to deliver accurate, explainable analytics. The | |
Use metric views in Genie Agents to enable accurate, explainable AI-powered analytics. | |
Monitor metric view measures and trigger notifications when thresholds are crossed. | |
Use the query profile to troubleshoot and optimize metric view queries. | |
Access metric views through JDBC and ODBC connections. Metadata APIs expose the metric view structure to external BI and analytics tools. | |
Query metric views from Tableau, Sigma, and other BI tools using custom SQL or BI compatibility mode. For Power BI, publish metric views as semantic models (Private Preview) or use custom SQL. | |
Query metric views from Microsoft Excel using the Databricks Excel Add-in. | |
Query metric views from Google Sheets using the Databricks connector. |