Prompt

How do I use SQL with a time-series database?

Technology / Databases1 observationLast seen Oct 25, 2026

Latest observation

Oct 25, 2026OpenAI APIWeb search: off

Using SQL with a time-series database is usually very similar to using SQL in a relational database, but with a few time-series-specific patterns and functions.

Basic idea

A time-series database stores data that changes over time, such as:

  • sensor readings
  • application metrics
  • financial prices
  • logs/events

You typically model each row as:

  • timestamp
  • entity/device/series id
  • measurement value(s)

Example table:

CREATE TABLE metrics (
  ts TIMESTAMP NOT NULL,
  device_id TEXT NOT NULL,
  temperature DOUBLE PRECISION,
  humidity DOUBLE PRECISION
);

Common SQL queries

1. Insert time-series data

INSERT INTO metrics (ts, device_id, temperature, humidity)
VALUES ('2026-10-25 12:00:00', 'device-1', 22.5, 40.2);

2. Query recent data

SELECT *
FROM metrics
WHERE device_id = 'device-1'
  AND ts >= NOW() - INTERVAL '1 hour'
ORDER BY ts;

3. Aggregate over time buckets

This is one of the most common time-series patterns.

SELECT
  DATE_TRUNC('minute', ts) AS minute,
  AVG(temperature) AS avg_temp
FROM metrics
WHERE device_id = 'device-1'
  AND ts >= NOW() - INTERVAL '1 day'
GROUP BY minute
ORDER BY minute;

Some databases provide special bucket functions, such as:

  • DATE_TRUNC(...)
  • time_bucket(...) in TimescaleDB
  • custom window/bucket functions in other TSDBs

4. Use window functions

To calculate moving averages or deltas:

SELECT
  ts,
  temperature,
  AVG(temperature) OVER (
    PARTITION BY device_id
    ORDER BY ts
    ROWS BETWEEN 4 PRECEDING AND CURRENT ROW
  ) AS moving_avg
FROM metrics
WHERE device_id = 'device-1';

5. Find latest value per device

SELECT DISTINCT ON (device_id) device_id, ts, temperature
FROM metrics
ORDER BY device_id, ts DESC;

Or using a window function:

SELECT device_id, ts, temperature
FROM (
  SELECT *,
         ROW_NUMBER() OVER (PARTITION BY device_id ORDER BY ts DESC) AS rn
  FROM metrics
) t
WHERE rn = 1;

Time-series-specific features to look for

Many time-series databases add SQL extensions for:

  • time bucketing (time_bucket, DATE_TRUNC)
  • downsampling / rollups
  • gap filling
  • retention policies
  • continuous aggregates/materialized views
  • time partitioning

Best practices

  • Always index or partition by timestamp
  • Often also index by the most common filter key, like device_id
  • Use append-only writes when possible
  • Avoid updating historical rows unless necessary
  • Pre-aggregate if queries are frequent over large ranges
  • Keep timestamps in UTC

Example in TimescaleDB

If you're using PostgreSQL + TimescaleDB:

SELECT time_bucket('5 minutes', ts) AS bucket,
       AVG(temperature)
FROM metrics
WHERE device_id = 'device-1'
GROUP BY bucket
ORDER BY bucket;

If you tell me the database

The exact SQL depends on the engine:

  • TimescaleDB
  • InfluxDB SQL/InfluxQL
  • ClickHouse
  • QuestDB
  • Apache Druid
  • PostgreSQL with time-series extensions

If you want, I can show:

  1. a schema design for a time-series table,
  2. example queries for a specific database, or
  3. how to write efficient time-series SQL.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.