# Create a report dataset

> Write the read-only query a report reads - choose the historian or the alarms, set the tag scope, declare the parameters and their defaults (today, yesterday, start_of_month(-1), now-24h), return the quality with each value, save through the server's checks and preview the rows as you.

Product: eaxScada · Type: How-to guide · Editions: Pro, Enterprise
Source: https://eaxware.com/docs/eaxscada/how-to/create-a-report-dataset/
Verified against commit e8c8e34.

## Goal

Describe the data of a report once, as a named query that anyone may run with their own read rights: for example the samples of area A for one day, or the hourly averages of the reactor temperatures with their quality.

You need the right `datasets_edit` ([Grant report rights](/docs/eaxscada/how-to/grant-report-rights/)). The rules behind each step are in [Analytics and Reports](/docs/eaxscada/explanation/analytics-and-reports/).
[Image: The Datasets tab with a new dataset, not saved yet: Name "Area A values", ID ds_area_a_values, Family historian, Tag scope Plant/A/>, the Parameters grid with from (timestamp, required, default yesterday) and to (timestamp, required, default today), and the Query editor holding SELECT ts, tag_path, value FROM samples ORDER BY tag_path, ts.]
[Image: The Datasets tab with a new dataset, not saved yet: Name "Area A values", ID ds_area_a_values, Family historian, Tag scope Plant/A/>, the Parameters grid with from (timestamp, required, default yesterday) and to (timestamp, required, default today), and the Query editor holding SELECT ts, tag_path, value FROM samples ORDER BY tag_path, ts.]

A new dataset. This query returns values without their quality: the server refuses to save it until <code>quality_class</code> is added (step 5).
## 1. Start a dataset

1. Open **Analytics and Reports** on the activity rail (or **Tools > Analytics and Reports**), then the **Datasets** tab.
2. Click **New dataset**.
3. Fill in:
   - **Name**: what readers of the report see, for example `Area A values`.
   - **ID**: `ds_` followed by lower-case letters, digits and `_`, for example `ds_area_a_values`. It is fixed once saved: report templates refer to it.
   - **Family**: the data the query reads, **historian** (tag samples) or **alarms** (the alarm journal and the alarm definitions). A family whose database this server does not have is marked "(not on this server)".
   - **Tag scope**: the tag paths or patterns the dataset may read, one per line, for example `Plant/A/>` (`>` matches everything below a folder, `*` one level). It is required. Each user's read rights apply on top of it: the scope never lets anyone read more than their rights allow.
   - **Description** (optional).

## 2. Declare the parameters

Every dataset has the time window **from** and **to**: two required timestamp parameters, already in the grid. The server only gives the query the rows of that window, so the query does not need to filter on time itself.

Click **Add parameter** for each other value the query needs: a name of lower-case letters, digits and `_` (not starting with `eax_`), a type, whether it is required, and a default. The types are `timestamp`, `duration`, `number`, `integer`, `text`, `bool`, `tag` (one tag path) and `tag_list` (several).

Defaults are written in a small fixed vocabulary, never as code:

| Default | Means |
|---|---|
| `now` | The time the run starts. |
| `today`, `yesterday` | The start of today, of yesterday, on the site's clock. |
| `start_of_day(n)`, `start_of_week(n)`, `start_of_month(n)`, `start_of_year(n)` | The start of the day, week (Monday), month or year `n` steps from the current one: `start_of_month(-1)` is the first of last month. |
| `+6h`, `-24h`, `-1d`, `+1w`, `-1h30m` after any of those | An offset. `h`, `m`, `s` are elapsed time; `d`, `w` are calendar days and weeks: `now-24h` and `now-1d` differ on a day with a clock change. |

For a daily dataset, `from` = `yesterday` and `to` = `today` is the usual pair. Defaults of the other types are plain values: `15m`, `42`, `R1`, `true`, a tag path, or tag paths separated by commas. The full list, with the shift words, is in the [reference](/docs/eaxscada/reference/analytics-and-reports/#parameter-defaults).

## 3. Write the query

The query is **one read-only `SELECT`** over the relations of the family:

- historian: `samples` (`ts`, `ts_ns`, `tag_path`, `value`, `quality`, `quality_class`, `simulated`);
- alarms: `alarm_events` (the journal in the window) and `alarm_definitions` (the current definitions).

The editor completes the relations, their columns, the quality helpers and your parameters. The columns of each relation are in the [reference](/docs/eaxscada/reference/analytics-and-reports/#relations-and-columns).

Read a parameter as `$name`. Values are passed to the database as parameters, never pasted into the text. Examples:

```sql
-- One tag's samples, with their quality
SELECT ts, tag_path, value, quality_class
FROM samples
WHERE tag_path = $tag
ORDER BY ts
```

```sql
-- Hourly averages of the good samples, with the quality of each hour
SELECT date_trunc('hour', ts) AS hour, tag_path,
       good_avg(value, quality_class) AS avg_value,
       good_count(quality_class)      AS good_samples,
       count(*)                       AS samples,
       agg_quality(quality_class)     AS quality
FROM samples
GROUP BY date_trunc('hour', ts), tag_path
ORDER BY hour, tag_path
```

```sql
-- Alarm events of the window, newest first
SELECT ts, alarm_id, name, severity, event_type, current_state, user_id, quality
FROM alarm_events
ORDER BY ts DESC
```

What the server refuses, on save and on every run:

- more than one statement, or anything but a `SELECT` (no `INSERT`, `COPY`, `ATTACH`, `SET`...);
- a name that is not one of the family's relations, and any name qualified with a schema or catalog (`main.samples`);
- table functions, files and URLs (`read_csv(...)`, `'file.parquet'`), and functions outside the allowed list;
- a query longer than 64 KiB.

## 4. Save

Click **Save** (**Ctrl+S**). The server checks the query before it stores anything: the rules above, the quality rule (next step), every default against this site's configuration, and a dry run as you, which records the columns the query returns. A report whose dataset later stops returning those columns fails with a clear reason instead of printing nonsense.

- An error the server can place is shown at its line in the editor; others are shown under the editor.
- If someone else saved the dataset since you opened it, the save is refused: reload it, then make your change again.
- Each save is a new revision (shown next to the name). Datasets are part of the project: they are in checkpoints and replicated to an HA standby.

## 5. Return the quality with each value

A historian dataset that returns sample values must also return their quality, or the save is refused with the reason (bad and uncertain samples would print as good ones) and a hint. Any one of these satisfies the rule:

- return `quality_class` (or `quality`) next to the value, and group by it when you aggregate;
- summarise the quality with `agg_quality(quality_class) AS quality` in an aggregate;
- keep the good samples only: `WHERE quality_class = 'good'`;
- average the good samples only with `good_avg(value, quality_class)`.

For a simple `SELECT`, **Add quality column** under the refusal adds `quality_class` for you; save again. The alarms family has no such rule: its relations carry a `quality` text column of their own.

## 6. Preview the rows

Click **Preview** (or press **Ctrl+Enter** in the query editor). It runs the **saved** dataset as you, so save first to preview a change. Enter parameter values, or leave them empty to take the defaults shown under each field.
[Image: The dataset preview: from and to left empty (default yesterday and today), 276 rows of Plant/A/T1 with the columns ts, tag_path, value and quality_class, a quick filter, Export CSV, and the chip "5 bad · 9 uncertain". The row at 01:50 is marked uncertain on an amber background.]
[Image: The dataset preview: from and to left empty (default yesterday and today), 276 rows of Plant/A/T1 with the columns ts, tag_path, value and quality_class, a quick filter, Export CSV, and the chip "5 bad · 9 uncertain". The row at 01:50 is marked uncertain on an amber background.]

The preview reads as you, up to 10,000 rows and 15 s. Bad rows are rose, uncertain rows amber.
The grid marks bad rows rose, uncertain rows amber and simulated values **SIM**. One chip above it sums up what to know: the quality counts, "First N rows only" when the preview limit cut the rows, and "N tags hidden by your read rights" when the tag scope covers tags you may not read. Another user may see fewer rows than you: a report always reads as the person who runs it.

## Delete a dataset

**Delete** in the row's actions asks first. A dataset that a saved or deployed report uses is not deleted: remove it from those reports, deploy, then delete it.

## Troubleshooting

| You see | Why | Do |
|---|---|---|
| The save is refused for the quality | The query returns values without quality. | Step 5: **Add quality column**, or one of the other forms. |
| A refusal that names a table, a schema or a function | The query names a table, a schema-qualified name, a file or a function outside the allowed list. | Read only `samples`, or `alarm_events` and `alarm_definitions`, without a schema. |
| A refusal about the time window | `from` or `to` was removed or is not a required timestamp. | Keep both as required timestamps. |
| A shift default (`this_shift`, `last_shift`) is refused | Shift defaults need the shift calendar, which this server does not have yet. | Use `today`, `yesterday` or `start_of_day(n)`. |
| The query is stopped for taking too long | The preview limit is 15 s (60 s for a report run). | Narrow the window or the tag scope, or aggregate. |
| The family shows "(not on this server)" | This server has no database for that family (for example a build without the columnar historian). | Edit the dataset on a server that has it. |

## Related

- [Design a report](/docs/eaxscada/how-to/design-a-report/)
- [Analytics and Reports reference](/docs/eaxscada/reference/analytics-and-reports/)
