Using roll ups and grouping sets

If your work involves heavy OLAP queries, you need to know about grouping sets and roll ups. Unified Analytics supports grouping sets with rollups and grouping functions.

You created a Unified Analytics Virtual Warehouse based on a Database Catalog that loads the sample data to run queries of airline data.
  1. Log into the Data Warehouse service as DWUser.
  2. Go to the Virtual Warehouses tab, locate the Virtual Warehouse using which you want to run queries, and click HUE.
    The Hue query editor opens in a new browser tab.
  3. Select a database.
  4. In Hue, select either the Impala or Hive editor.
  5. Run a query that determines the number of flights per country and state.
    SELECT
      a.country,
      a.state,
      grouping(a.country)
      grouping(a.state)
      SUM(f.Distance),
      COUNT(*)
    FROM
      flights f,
      airports a
    WHERE
      f.origin = a.iata
    GROUP BY
      a.country,
      a.state
    WITH ROLLUP;
  6. Run a query that defines custom grouping sets: country and country/state.
    SELECT
      a.country,
      a.state,
      grouping(a.country)
      grouping(a.state)
      SUM(f.Distance),
      COUNT(*)
    FROM
      flights f,
      airports a
    WHERE
      f.origin = a.iata
    GROUP BY
      a.country,
      a.state
    GROUPING SETS (
      (a.country),
      (a.country, a.state)
    );