Skip to content

Instantly share code, notes, and snippets.

@droserasprout
Created August 9, 2021 20:01
Show Gist options
  • Select an option

  • Save droserasprout/43b1f4100e14549c8a36c5a82d518237 to your computer and use it in GitHub Desktop.

Select an option

Save droserasprout/43b1f4100e14549c8a36c5a82d518237 to your computer and use it in GitHub Desktop.
PostgreSQL time series without TimescaleDB
CREATE MATERIALIZED VIEW users_30m AS
SELECT
a.exchange_id,
COUNT(DISTINCT a.trader_id) as users,
to_timestamp(
floor(
(
extract(
'epoch'
from
a.timestamp
) / 1800
)
) * 1800
) AT TIME ZONE 'UTC' as bucket
FROM
dex.activity a
GROUP BY
bucket,
a.exchange_id;
CREATE MATERIALIZED VIEW users_1w AS
SELECT
a.exchange_id,
COUNT(DISTINCT a.trader_id) as users,
date_trunc('week', a.timestamp) as bucket
FROM
dex.activity a
GROUP BY
bucket,
a.exchange_id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment