3

PostgreSQL: 13.4
TimescaleDB: 2.4.2

When creating a view that is based on an existing view I cannot GROUP BY a time_bucket field. In the following example you can see three rows each at 20:00:00, those should be one row with the traffic tallied up. The 5min View does that as intended for the raw data.

As an example, here I tally the traffic in a 5min time_bucket (irrelevant data omitted from pictures):

enter image description here

With:

CREATE MATERIALIZED VIEW wifi_traffic_5m
WITH (timescaledb.continuous)
AS
    SELECT
        time_bucket('5 minutes', "timestamp") AS bucket_time,
        clientid,
        customer,
        devicecpuid,
        SUM(traffic) AS traffic
    FROM ht_wifi_traffic_processed
    GROUP BY bucket_time, clientid, customer, devicecpuid

Now I wanted to create a 15min view based on the 5min view, but I get this result:

enter image description here

With:

CREATE VIEW wifi_traffic_15m
AS
    SELECT
        time_bucket('15 minutes', bucket_time) AS bucket_time,
        clientid,
        customer,
        devicecpuid,
        SUM(traffic) AS traffic
    FROM wifi_traffic_5m
    GROUP BY bucket_time, clientid, customer, devicecpuid
Splitframe
  • 406
  • 3
  • 16

1 Answers1

4

Well shortly after posting the question I found the solution myself. The name of the time bucket cannot be the same in both views.

If I edit the second view as follows:

CREATE VIEW wifi_traffic_15m
AS
    SELECT
        time_bucket('15 minutes', bucket_time) AS new_bucket_time,
        clientid,
        customer,
        devicecpuid,
        SUM(traffic) AS traffic
    FROM wifi_traffic_5m
    GROUP BY new_bucket_time, clientid, customer, devicecpuid

It works as intended.

Splitframe
  • 406
  • 3
  • 16