Query data from PostgreSQL to represent it in a time series graph

Diario del capitán, fecha estelar d384.y40/AB

In this blog post, I will show you how to query data from PostgreSQL to represent it in a time series graph.

Net - Photo by Uriel SC on Unsplash

If we have a table named historic with the following structure:

    id: integer
    timestamp: timestamp
    value: integer

We can query this data by intervals by using the following query:

        generate_series(timestamp '2023-05-01', timestamp '2023-07-01', interval '1 day') AS day_timestamp
        LEFT OUTER JOIN historic ON (date(historic.timestamp) = date(day_timestamp))

Here, I am joining data between 2023-05-01 to 2023-07-01 from the historic table and I am performing an AVG in case there is more than one record in the same day. We can use any other aggregation function instead, like COUNT, SUM, etc.

The important function of this table is the generate_series that allows me to generate records between two values and a period. By joining and grouping the resulting data, we get the desired results.

If we want to group by months instead, then we just need to change the generate_series interval to 1 month and change the conditions in the LEFT OUTER JOIN to compare between the month and the year of the dates.


This query might not be performant for millions of historic records, but for small to medium sized projects, this should be more than enough.

To optimise this query we can create functional indices to index the timestamp in its date, month or year forms, or store the month and year as separate columns in the database and add indices for them.

Xavier Redó

Xavier Redó

Responsable de arquitectura y desarrollo. Es el culpable de que trabajemos con Ruby on Rails, Node.js, React y Angular, entre otros. Pocos desarrolladores que conozcamos tienen sus habilidades sociales.

comments powered by Disqus

Estás a un paso de conocer a tu mejor socio.