26 lines
1.3 KiB
SQL
26 lines
1.3 KiB
SQL
-- ?1 selects an event's visible conditions; ?2 selects a single market.
|
|
WITH selected(market) AS MATERIALIZED (
|
|
SELECT s.market FROM listed_markets s
|
|
LEFT JOIN augmented_outcomes a ON a.market=s.market
|
|
WHERE s.event=?1 AND (a.role IS NULL OR a.role<>'placeholder')
|
|
UNION ALL SELECT id FROM markets WHERE id=?2
|
|
), executions AS (
|
|
SELECT s.market, f.seq, f.quantity, p.at_ms, f.price AS taker_price
|
|
FROM selected s CROSS JOIN orders o ON o.market=s.market AND o.side='buy'
|
|
CROSS JOIN fills f ON f.bid=o.seq LEFT JOIN trade_prices p ON p.fill=f.seq
|
|
UNION ALL
|
|
SELECT s.market, f.seq, f.quantity, f.at_ms,
|
|
CASE c.taker_order WHEN f.yes_order THEN f.yes_price
|
|
WHEN f.no_order THEN 10000-f.yes_price END
|
|
FROM selected s CROSS JOIN paired_fills f ON f.market=s.market
|
|
LEFT JOIN execution_context c ON c.execution=f.seq
|
|
)
|
|
SELECT s.market, e.at_ms BETWEEN ?3-86400000 AND ?3 AS recent,
|
|
COALESCE(SUM(e.quantity),0),
|
|
COALESCE(SUM(e.quantity) FILTER (WHERE c.taker_order IS NOT NULL),0),
|
|
COALESCE(SUM(e.quantity*e.taker_price) FILTER (WHERE c.taker_order IS NOT NULL),0),
|
|
COALESCE(SUM(e.quantity) FILTER (WHERE c.execution IS NULL),0)
|
|
FROM selected s LEFT JOIN executions e ON e.market=s.market
|
|
LEFT JOIN execution_context c ON c.execution=e.seq
|
|
GROUP BY s.market,recent
|