Code:
CREATE TABLE test(
a_pkey SERIAL NOT NULL PRIMARY KEY,
event_id VARCHAR(12) NOT NULL,
full_description VARCHAR(100) NOT NULL,
course VARCHAR(12) NOT NULL,
scheduled_off VARCHAR(12) NOT NULL,
event VARCHAR(36) NOT NULL,
actual_off VARCHAR(12) NOT NULL,
tabno SMALLINT NOT NULL,
selection VARCHAR(36) NOT NULL,
settled_date DATE NOT NULL,
odds REAL NOT NULL,
latest_taken VARCHAR(12) NOT NULL,
first_taken VARCHAR(12) NOT NULL,
in_play VARCHAR(12) NOT NULL,
number_bets INTEGER NOT NULL,
volume_matched REAL NOT NULL,
sports_id VARCHAR(12) NOT NULL,
selection_id VARCHAR(12) NOT NULL,
win_flag SMALLINT NOT NULL
);
COPY test (event_id, full_description, course, scheduled_off, event, actual_off, tabno, selection, settled_date, odds, latest_taken, first_taken, in_play, number_bets, volume_matched, sports_id, selection_id, win_flag)
FROM 'E:/downloads/change.csv'
csv
SELECT DISTINCT full_description, event, selection, win_flag, first_value(odds) OVER w / last_value(odds) OVER w AS chg
FROM test
WINDOW w AS (PARTITION BY selection_id ORDER BY a_pkey RANGE
BETWEEN UNBOUNDED PRECEDING
AND UNBOUNDED FOLLOWING)
"Coro (AUS) 7th Jul";"R1 1000m Mdn";" Nuclear Power";0;1.42857
"Coro (AUS) 7th Jul";"R1 1000m Mdn";" Diamond Charlie";0;0.806452
"Coro (AUS) 7th Jul";"R1 1000m Mdn";" Golden Sally";1;1.50943