Skip to content

Instantly share code, notes, and snippets.

View stefanoc's full-sized avatar

Stefano Cobianchi stefanoc

View GitHub Profile
@stefanoc
stefanoc / query.sql
Last active June 21, 2016 11:37
Window function example
truncate table events;
CREATE TABLE events(ref_id integer, event_type text, ts integer);
INSERT INTO events (ref_id, event_type, ts) VALUES (1, 'foo', 1), (1, 'bar', 1), (2, 'foo', 0), (3, 'foo', 0), (1, 'foo', 4), (3, 'foo', 7), (2, 'bar', 10);
select
ref_id,
event_type,
ts,
first_value(ts) over (
partition by ref_id, event_type order by ts asc rows between 1 following and unbounded following