# GROUP BY

> Split the numbers by fields, related fields, roles, and time units, and keep only the biggest groups with TOP.

`GROUP BY` splits the numbers into rows, one for each value of a field.

```sql
FROM deals
SHOW count, won_value
GROUP BY owner, stage
```

## Syntax

```sql
GROUP BY [ONLY] [TOP <n>] <field> [OVERALL][, <field> ...]
```

| Grouping | Example |
|---|---|
| A field of the object | `GROUP BY stage`, `GROUP BY owner` |
| A custom property | `GROUP BY customer_tier`, `GROUP BY "Customer tier"` |
| A related object's field | `GROUP BY accounts.industry` |
| One specific reference | `GROUP BY decision_maker.title` |
| A time unit | `GROUP BY month`, `GROUP BY day_of_week` |

## Time units

`day`, `week`, `month`, `quarter`, `year`, `day_of_week`, `hour_of_day`, `week_of_year`, and `month_of_year`.

`GROUP BY month` lists only the months that have data. To list every month in the period, with zeros where nothing happened, use [TIMESERIES](https://workstation.repzo.com/docs/analytics/repzoql/timeseries).

## Related fields and roles

`accounts.industry` follows every link between the record and accounts. To follow one specific reference instead, write the reference's name and a dot, for example a deal's decision maker:

```sql
FROM deals
SHOW count
GROUP BY decision_maker.title
```

## TOP

Keep the biggest groups and fold the rest into an **Other** row:

```sql
FROM deals
SHOW won_value
GROUP BY TOP 5 owner
```

| Form | Result |
|---|---|
| `TOP 5 owner` | The five biggest owners, plus Other |
| `ONLY TOP 5 owner` | The five biggest owners, no Other row |
| `TOP 5 owner OVERALL` | With two groupings, ranks owners across everything instead of within each value of the first grouping |

Groups are ranked by the first metric you show.

## Related pages

- [TIMESERIES](https://workstation.repzo.com/docs/analytics/repzoql/timeseries)
- [HAVING](https://workstation.repzo.com/docs/analytics/repzoql/having)
- [ORDER BY and LIMIT](https://workstation.repzo.com/docs/analytics/repzoql/order-by-and-limit)
