SELECT data

You can query data from a hypertable using a standard SELECT command. All SQL clauses and features are supported.

Basic query examples

Here are some examples of basic SELECT queries.

Return the 100 most-recent entries in the table conditions. Order the rows from newest to oldest:

2. SELECT * FROM conditions ORDER BY time DESC LIMIT 100;

Return the number of entries written to the table conditions in the last 12 hours:

2. SELECT COUNT(*) FROM conditions

4. WHERE time > NOW() - INTERVAL '12 hours';

Advanced query examples

Here are some examples of more advanced SELECT queries.

Get information about the weather conditions at each location, for each 15-minute period within the last 3 hours. Calculate the number of measurements taken, the maximum temperature, and the maximum humidity. Order the results by maximum temperature.

This examples uses the time_bucket function to aggregate data into 15-minute buckets:

2. SELECT time_bucket('15 minutes', time) AS fifteen_min,

4. location,

6. COUNT(*),

8. MAX(temperature) AS max_temp,

10. MAX(humidity) AS max_hum

12. FROM conditions

14. WHERE time > NOW() - INTERVAL '3 hours'

16. GROUP BY fifteen_min, location

18. ORDER BY fifteen_min DESC, max_temp DESC;

Count the number of distinct locations with air conditioning that have reported data in the last day:

2. SELECT COUNT(DISTINCT location) FROM conditions

4. JOIN locations

6. ON conditions.location = locations.location

8. WHERE locations.air_conditioning = True

10. AND time > NOW() - INTERVAL '1 day';