SELECT commands

Data can be queried from a hypertable using the standard SELECT SQL command (PostgreSQL docs), including with arbitrary WHERE clauses, GROUP BY and ORDER BY commands, joins, subqueries, window functions, user-defined functions (UDFs), HAVING clauses, and so on.

From basic queries:

  1. -- Return the most recent 100 entries in the table 'conditions' ordered newest to oldest
  2. SELECT * FROM conditions ORDER BY time DESC LIMIT 100;
  3. -- Return the number of data entries written in past 12 hours
  4. SELECT COUNT(*) FROM conditions
  5. WHERE time > NOW() - INTERVAL '12 hours';

To more advanced SQL queries:

  1. -- Information about each 15-min period for each location
  2. -- over the past 3 hours, ordered by time and temperature
  3. SELECT time_bucket('15 minutes', time) AS fifteen_min,
  4. location, COUNT(*),
  5. MAX(temperature) AS max_temp,
  6. MAX(humidity) AS max_hum
  7. FROM conditions
  8. WHERE time > NOW() - INTERVAL '3 hours'
  9. GROUP BY fifteen_min, location
  10. ORDER BY fifteen_min DESC, max_temp DESC;
  11. -- How many distinct locations with air conditioning
  12. -- have reported data in the past day
  13. SELECT COUNT(DISTINCT location) FROM conditions
  14. JOIN locations
  15. ON conditions.location = locations.location
  16. WHERE locations.air_conditioning = True
  17. AND time > NOW() - INTERVAL '1 day'