# Force a designated timestamp

Learn how to explicitly set a designated timestamp column in QuestDB queries using the TIMESTAMP keyword

Sometimes you need to force a designated timestamp in your query. This happens when you want to run operations like `SAMPLE BY` with a non-designated timestamp column, or when QuestDB applies certain functions or joins and loses track of the designated timestamp.

## Problem: Lost designated timestamp

When you run this query on the demo instance, you'll notice the `time` column is not recognized as a designated timestamp because we cast it to a string and back:

```questdb-sql demo title="Query without designated timestamp"
SELECT
  TO_TIMESTAMP(timestamp::STRING, 'yyyy-MM-ddTHH:mm:ss.SSSUUUZ') time,
  symbol,
  ecn,
  bid_price
FROM
  core_price
WHERE timestamp IN '$today'
LIMIT 10;
```

Without a designated timestamp, you cannot use time-series operations like `SAMPLE BY`.

## Solution: Use the TIMESTAMP keyword

You can force the designated timestamp using the `TIMESTAMP()` keyword, which allows you to run time-series operations:

```questdb-sql demo title="Force designated timestamp with TIMESTAMP keyword"
WITH t AS (
  (
    SELECT
      TO_TIMESTAMP(timestamp::STRING, 'yyyy-MM-ddTHH:mm:ss.SSSUUUZ') time,
      symbol,
      ecn,
      bid_price
    FROM
      core_price
    WHERE timestamp IN '$now - 1h..$now'
    ORDER BY time
  ) TIMESTAMP (time)
)
SELECT * FROM t LATEST BY symbol;
```

The `TIMESTAMP(time)` clause explicitly tells QuestDB which column to use as the designated timestamp, enabling `LATEST BY` and other time-series operations. This query gets the most recent price for each symbol in the last hour.

## Common case: UNION queries

The designated timestamp is often lost when using `UNION` or `UNION ALL`. This is intentional - QuestDB cannot guarantee that the combined results are in order, and designated timestamps must always be in ascending order.

You can restore the designated timestamp by applying `ORDER BY` and then using `TIMESTAMP()`:

```questdb-sql demo title="Restore designated timestamp after UNION ALL"
(
  SELECT * FROM
  (
    SELECT timestamp, symbol FROM core_price WHERE timestamp IN '$now - 1m..$now'
    UNION ALL
    SELECT timestamp, symbol FROM core_price WHERE timestamp IN '$now - 1m..$now'
  ) ORDER BY timestamp
)
TIMESTAMP(timestamp)
LIMIT 10;
```

This query combines the last minute of data twice using `UNION ALL`, then restores the designated timestamp.

## Querying external Parquet files

When querying external parquet files using `read_parquet()`, the result does not have a designated timestamp. You need to force it using `TIMESTAMP()` to enable time-series operations like `SAMPLE BY`:

```questdb-sql demo title="Query parquet file with designated timestamp"
SELECT timestamp, avg(price)
FROM ((read_parquet('trades.parquet') ORDER BY timestamp) TIMESTAMP(timestamp))
SAMPLE BY 1m;
```

This query reads from a parquet file, applies ordering, forces the designated timestamp, and then performs time-series aggregation.

:::warning Order is Required
The `TIMESTAMP()` keyword requires that the data is already sorted by the timestamp column. If the data is not in order, the query will fail. Always include `ORDER BY` before applying `TIMESTAMP()`.
:::

:::info Related Documentation
- [Designated Timestamp concept](/docs/concepts/designated-timestamp/)
- [TIMESTAMP keyword reference](/docs/query/sql/select/#timestamp)
- [SAMPLE BY aggregation](/docs/query/sql/sample-by/)
- [Parquet functions](/docs/query/functions/parquet/)
:::
