Time-Series Examples
Time-Series Examples
Version: 6.0
This document provides practical code examples for common time-series use cases in HeliosDB.
SQL Examples
Create Time-Series Tables
-- IoT sensor readings tableCREATE TABLE sensor_readings ( timestamp TIMESTAMPTZ NOT NULL, sensor_id VARCHAR(64) NOT NULL, facility VARCHAR(32) NOT NULL, temperature DOUBLE PRECISION, humidity DOUBLE PRECISION, pressure DOUBLE PRECISION, vibration DOUBLE PRECISION) WITH ( partition_by = 'HOUR', retention = '7 days', compression = 'gorilla');
-- Create continuous aggregate for hourly averagesCREATE MATERIALIZED VIEW sensor_hourly ASSELECT time_bucket('1 hour', timestamp) AS bucket, sensor_id, facility, AVG(temperature) AS avg_temp, AVG(humidity) AS avg_humidity, AVG(pressure) AS avg_pressure, AVG(vibration) AS avg_vibration, COUNT(*) AS sample_countFROM sensor_readingsGROUP BY bucket, sensor_id, facilityWITH ( continuous = true, refresh_interval = '5 minutes');Query Patterns
-- Last hour of raw dataSELECT timestamp, sensor_id, temperature, humidityFROM sensor_readingsWHERE timestamp > NOW() - INTERVAL '1 hour' AND sensor_id = 'sensor-001'ORDER BY timestamp;
-- Hourly averages for last 24 hoursSELECT time_bucket('1 hour', timestamp) AS hour, AVG(temperature) AS avg_temp, MAX(temperature) AS max_temp, MIN(temperature) AS min_tempFROM sensor_readingsWHERE timestamp > NOW() - INTERVAL '24 hours' AND sensor_id = 'sensor-001'GROUP BY hourORDER BY hour;
-- Gap filling with linear interpolationSELECT time_bucket_gapfill('5 minutes', timestamp) AS bucket, sensor_id, interpolate(avg(temperature)) AS temperatureFROM sensor_readingsWHERE timestamp BETWEEN '2025-01-01 00:00:00' AND '2025-01-01 12:00:00'GROUP BY bucket, sensor_idORDER BY bucket, sensor_id;
-- Detect anomalies (values > 3 standard deviations)WITH stats AS ( SELECT sensor_id, AVG(temperature) AS mean, STDDEV(temperature) AS stddev FROM sensor_readings WHERE timestamp > NOW() - INTERVAL '1 day' GROUP BY sensor_id)SELECT r.timestamp, r.sensor_id, r.temperature, (r.temperature - s.mean) / s.stddev AS z_scoreFROM sensor_readings rJOIN stats s ON r.sensor_id = s.sensor_idWHERE timestamp > NOW() - INTERVAL '1 hour' AND ABS(r.temperature - s.mean) > 3 * s.stddevORDER BY timestamp;
-- Moving averageSELECT timestamp, temperature, AVG(temperature) OVER ( ORDER BY timestamp ROWS BETWEEN 9 PRECEDING AND CURRENT ROW ) AS moving_avg_10FROM sensor_readingsWHERE sensor_id = 'sensor-001' AND timestamp > NOW() - INTERVAL '1 hour';
-- Rate of change per secondSELECT timestamp, temperature, (temperature - LAG(temperature) OVER (ORDER BY timestamp)) / EXTRACT(EPOCH FROM (timestamp - LAG(timestamp) OVER (ORDER BY timestamp))) AS rate_per_secondFROM sensor_readingsWHERE sensor_id = 'sensor-001' AND timestamp > NOW() - INTERVAL '1 hour';
-- OHLCV bars for financial dataSELECT time_bucket('5 minutes', timestamp) AS bucket, symbol, FIRST(price, timestamp) AS open, MAX(price) AS high, MIN(price) AS low, LAST(price, timestamp) AS close, SUM(volume) AS volumeFROM market_ticksWHERE timestamp > NOW() - INTERVAL '1 day' AND symbol = 'AAPL'GROUP BY bucket, symbolORDER BY bucket;
-- Session-based analysisWITH sessions AS ( SELECT user_id, timestamp, SUM(CASE WHEN timestamp - LAG(timestamp) OVER ( PARTITION BY user_id ORDER BY timestamp ) > INTERVAL '30 minutes' THEN 1 ELSE 0 END) OVER (PARTITION BY user_id ORDER BY timestamp) AS session_id FROM user_events WHERE timestamp > NOW() - INTERVAL '1 day')SELECT user_id, session_id, MIN(timestamp) AS session_start, MAX(timestamp) AS session_end, COUNT(*) AS event_countFROM sessionsGROUP BY user_id, session_idORDER BY user_id, session_start;Retention and Downsampling via SQL
-- Set retention policyALTER TABLE sensor_readingsSET RETENTION POLICY '30 days';
-- Create downsampling policyCREATE DOWNSAMPLING POLICY ON sensor_readings EVERY '1 hour' WITH AGGREGATION ( temperature = AVG, humidity = AVG, pressure = AVG, vibration = AVG ) AFTER '7 days' RETAIN '365 days';
-- Force immediate compactionVACUUM (COMPACT) sensor_readings;See Also: README | Quick Start | User Guide