Browse Source

postgres query

tags/v0.2.0
Jonathan Strong 4 years ago
parent
commit
6e12583400
1 changed files with 30 additions and 0 deletions
  1. +30
    -0
      pg-size-wt-avg-query.sql

+ 30
- 0
pg-size-wt-avg-query.sql View File

@@ -0,0 +1,30 @@
SELECT hr,
bmex / gdax AS ratio
FROM
(SELECT hr,
max(gdax) "gdax", -- max(..) necessary to grab the non-NULL row from two returned
max(bmex) "bmex" -- rows for each hr
FROM
(SELECT hr,
(CASE
WHEN exch=3 THEN wt_avg
END) "gdax",
(CASE
WHEN exch=6 THEN wt_avg
END) "bmex"
FROM
(SELECT hr,
exch,
w_sum / sum_w AS wt_avg
FROM
(SELECT date_trunc('hour', "time") AS hr,
exch,
sum(price * amount) AS w_sum,
sum(amount) AS sum_w
FROM trades
WHERE base=1 -- btc=1 usd=100
AND quote=100
AND (exch=3 OR exch=6) -- gdax=3, bmex=6
GROUP BY 1, 2
ORDER BY 1, 2) a) b) c
GROUP BY hr) d;

Loading…
Cancel
Save