Skip to content

Commit ccf47ea

Browse files
committed
1 parent 1f8851e commit ccf47ea

File tree

2 files changed

+26
-0
lines changed
  • pragma-entities/migrations/2024-02-20-090454_fix_twap_aggs

2 files changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- This file should undo anything in `up.sql`
2+
ALTER MATERIALIZED VIEW twap_2_hours_agg SET SCHEMA (
3+
SELECT
4+
pair_id,
5+
time_bucket('2 hours'::interval, timestamp) as bucket,
6+
average(time_weight('Linear', timestamp, price))::numeric as price_twap,
7+
COUNT(DISTINCT source) as num_sources
8+
FROM entries
9+
GROUP BY bucket, pair_id
10+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- Your SQL goes here
2+
ALTER MATERIALIZED VIEW twap_2_hours_agg SET SCHEMA (
3+
SELECT
4+
pair_id,
5+
time_bucket('2 hours'::interval, timestamp) as bucket,
6+
interpolated_average(
7+
time_weight('Linear', timestamp, price),
8+
bucket,
9+
'2 hours'::interval,
10+
lag(agg) OVER (ORDER BY bucket),
11+
lead(agg) OVER (ORDER BY bucket)
12+
)::numeric as price_twap,
13+
COUNT(DISTINCT source) as num_sources
14+
FROM entries
15+
GROUP BY bucket, pair_id
16+
)

0 commit comments

Comments
 (0)