'Select Evenly Spaced Rows Between 2 timestamptz

I'm trying to select N rows between 2 timestamptz columns and have them be spaced out evenly. I'm logging temperature and humidity every 5 seconds and I want to create a chart using chartjs for a time period(say last 24 hours)

My table looks like:

CREATE TABLE IF NOT EXISTS temperatures
(
    id          BIGSERIAL   PRIMARY KEY,
    timestamp   TIMESTAMPTZ NOT NULL,
    temp_f      REAL        NOT NULL,
    humidity    REAL        NOT NULL
);

In English the query would read:
SELECT 24 evenly spaced rows FROM temperatures table WHERE timestamp BETWEEN '2022-04-20 00:00:00' AND '2022-04-21 00:00:00' with the first and last rows as close to specified times as possible

This would return something like:

 timestamp            |  temp_f   | humidity
-------------------------------+-----------+
 2022-04-20 00:00:05  |      70.7 |     54.2
 2022-04-20 01:00:03  |     72.32 |     53.9
 ...
 ...
 2022-04-20 23:59:59  |     72.32 |     53.4
...

From my own searching it seems like generate_series might work, but I cannot figure out how to map the generated series to my table.

Here is a dbfiddle https://www.db-fiddle.com/f/8vakciTyFvb5PQniwuwanQ/0



Solution 1:[1]

In English what parameter will make rows even? Are they separated by time, number of rows, value of some column, or something else? What is your denominator to space out rows?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user15411679