cube function

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime

Creates a multi-dimensional cube using the specified expression columns.


cube (expr1 [, ...] )


  • exprN: Any expression that can be grouped.


The function must be the only grouping expression in the GROUP BY clause. See GROUP BY clause for details.


> SELECT name, age, count(*) FROM VALUES (2, 'Alice'), (5, 'Bob') people(age, name) GROUP BY cube(name, age);
  Bob   5       1
  Alice 2       1
  Alice NULL    1
  NULL  2       1
  NULL  NULL    2
  Bob   NULL    1
  NULL  5       1