> ## Documentation Index
> Fetch the complete documentation index at: https://docs.stockful.app/llms.txt
> Use this file to discover all available pages before exploring further.

# GROUP BY

> Break the results down by one or more dimensions

`GROUP BY` collapses your rows into groups and aggregates each metric within them. List one or more dimensions, separated by commas, and every metric in `SHOW` is totalled per group.

```sql theme={null}
FROM sales
  SHOW total_revenue, units_sold
  GROUP BY vendor
```

Grouping by two dimensions gives you a breakdown within a breakdown, such as product type within vendor.

```sql theme={null}
FROM sales
  SHOW total_revenue
  GROUP BY vendor, product_type
```

## Top N per dimension

Prefix a dimension with `ONLY TOP <n>` to keep just its highest-ranking values. This is handy on a two-dimension breakdown where you want, say, the top five vendors but every product type within them.

```sql theme={null}
FROM sales
  SHOW total_revenue
  GROUP BY ONLY TOP 5 vendor, product_type
```

Which dimensions are available depends on the dataset. To filter groups by their totals after aggregating, use [HAVING](/stockfulql/syntax/having). To sort the grouped results, use [ORDER BY](/stockfulql/syntax/order-by).
