Skip to content

Instantly share code, notes, and snippets.

@knewter
Last active December 16, 2015 23:48
Show Gist options
  • Save knewter/5515990 to your computer and use it in GitHub Desktop.
Save knewter/5515990 to your computer and use it in GitHub Desktop.
start_time_tz | end_time_tz | volume | low | high | first | last
------------------------+------------------------+-----------------+---------------+---------------+---------------+---------------
2011-06-26 12:15:00-05 | 2011-06-26 12:20:00-05 | 2.0000000000 | 17.5100100000 | 17.5100100000 | 17.5100100000 | 17.5100100000
2011-06-26 12:30:00-05 | 2011-06-26 12:35:00-05 | 2.0000000000 | 17.5100100000 | 17.5100100000 | 17.5100100000 | 17.5100100000
2011-06-26 12:40:00-05 | 2011-06-26 12:45:00-05 | 1.0000000000 | 17.5100100000 | 17.5100100000 | 17.5100100000 | 17.5100100000
2011-06-26 12:45:00-05 | 2011-06-26 12:50:00-05 | 5.0699297300 | 15.0000000000 | 15.0000000000 | 15.0000000000 | 15.0000000000
2011-06-26 12:50:00-05 | 2011-06-26 12:55:00-05 | 5.0000000000 | 16.5000000000 | 16.5000000000 | 16.5000000000 | 16.5000000000
2011-06-26 12:55:00-05 | 2011-06-26 13:00:00-05 | 50.0000000000 | 16.5000000000 | 17.0000000000 | 16.5000000000 | 17.0000000000
2011-06-26 13:00:00-05 | 2011-06-26 13:05:00-05 | 13.8700000000 | 15.1000000000 | 15.1000000000 | 15.1000000000 | 15.1000000000
2011-06-26 13:25:00-05 | 2011-06-26 13:30:00-05 | 0.1090000000 | 15.0000000000 | 15.0000000000 | 15.0000000000 | 15.0000000000
2011-06-26 13:40:00-05 | 2011-06-26 13:45:00-05 | 5.0000000000 | 16.0000000000 | 16.0000000000 | 16.0000000000 | 16.0000000000
select to_timestamp(start_time * 300) start_time_tz, to_timestamp((start_time + 1) * 300) end_time_tz, sum(amount) as volume, min(price) as low, max(price) as high, max(first_price) as first, max(last_price) as last
from (
select date/300 as start_time, amount, price, first_value(price) over w as first_price, last_value(price) over w as last_price
from trades
window w as (partition by date/300 order by date rows between unbounded preceding and unbounded following)
) s
group by start_time
order by start_time;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment