# Group rows before forecasting

> For AI agents: the complete documentation index is at
> [https://docs.retrocast.com/llms.txt](https://docs.retrocast.com/llms.txt) — every page is also available as
> markdown by appending `.md` to its URL.

Your table is one row per store per day, but you want a forecast per city —
every store in that city summed together. Declare the grouping in
`hierarchies`, then pick the level you want with `selector`:

```json
{
  "input": {
    "source": {
      "inline": {
        "date":    ["2024-01-01", "2024-01-01", "2024-01-01", "2024-01-02", "2024-01-02", "2024-01-02"],
        "store":   ["S1", "S2", "S1", "S1", "S2", "S1"],
        "city":    ["Paris", "Lyon", "Paris", "Paris", "Lyon", "Paris"],
        "channel": ["online", "online", "in_store", "online", "online", "in_store"],
        "sales":   [42, 30, 58, 40, 28, 62]
      }
    },
    "columns": {
      "date":    { "kind": "time", "frequency": "1d" },
      "store":   "identifier",
      "city":    "identifier",
      "channel": "identifier",
      "sales":   { "kind": "target", "aggregate": "sum" }
    },
    "hierarchies": {
      "store": [["store", "city"]]
    }
  },
  "selector": { "city": "any" },
  "prediction_length": "1w"
}
```

Post it to `https://api.retrocast.com/v1-beta/forecast?model=t0-alpha`.
`model` names the forecasting model and is required — without it the
request is rejected with `missing field "model"`.

What each part does:

- `hierarchies` says `store` sits inside `city`.
- `selector: { "city": "any" }` asks for one forecast per city. Every store
  in a city is summed into it.
- `channel` appears in neither `hierarchies` nor `selector`, so it is summed
  over too — `online` and `in_store` are added together.

To get fewer series back, name the values instead of `"any"`:
`{ "city": ["Paris"] }` returns Paris only, still summed across its stores
and channels.

You don't need to reshape your table or keep a separate rollup table. Send
the rows you have and pick the level at request time.

## What comes back

One row per (city, time step) — two cities over seven days, so 14 rows,
trimmed here to the first two:

```json
{
  "data": {
    "time":      ["2024-01-03T00:00:00Z", "2024-01-04T00:00:00Z"],
    "cutoff":    ["2024-01-02T00:00:00Z", "2024-01-02T00:00:00Z"],
    "lead_time": ["PT0S", "P1D"],
    "span":      ["P1D", "P1D"],
    "identifiers": { "city": ["Lyon", "Lyon"] },
    "targets": {
      "sales": {
        "quantiles": {
          "0.1":  [26.64, 26.68],
          "0.5":  [28.48, 28.71],
          "0.9":  [31.47, 32.40],
          "mean": [28.68, 29.00]
        }
      }
    }
  }
}
```

`identifiers` carries only the dimensions that identify a row — here `city`
alone. `store` and `channel` are absent: those rows were summed together, so
there is no single value to report.

The request above sets no `quantiles`, so the response comes back with a
default spread that includes `mean`. Set `quantiles` yourself and you get
exactly the levels you asked for.
