I am trying to find a way to compare with the current row in the PARTITION BY clause in the WINDOW function in a PostgreSQL query.
Imagine that I have a short list in the next query of these 5 elements (in the real case, I have thousands or even millions of lines). I am trying to get, for each row, the identifier of the next other element (event column) and the identifier of the previous other element.
WITH events AS( SELECT 1 as id, 12 as event, '2014-03-19 08:00:00'::timestamp as date UNION SELECT 2 as id, 12 as event, '2014-03-19 08:30:00'::timestamp as date UNION SELECT 3 as id, 13 as event, '2014-03-19 09:00:00'::timestamp as date UNION SELECT 4 as id, 13 as event, '2014-03-19 09:30:00'::timestamp as date UNION SELECT 5 as id, 12 as event, '2014-03-19 10:00:00'::timestamp as date ) SELECT lag(id) over w as previous_different, event , lead(id) over w as next_different FROM events ev WINDOW w AS (PARTITION BY event!=ev.event ORDER BY date ASC);
I know that comparing event!=ev.event is wrong, but what I want to achieve.
The result you get (the same as if I deleted the PARTITION BY clause):
|12|2 1|12|3 2|13|4 3|13|5 4|12|
And I want to get the result:
|12|3 |12|3 2|13|5 2|13|5 4|12|
Does anyone know if this is possible and how? Thank you very much!
EDIT: I know I can do this with two JOIN s, a ORDER BY and a DISTINCT ON , but in the real case of millions of rows, this is very inefficient:
WITH events AS( SELECT 1 as id, 12 as event, '2014-03-19 08:00:00'::timestamp as date UNION SELECT 2 as id, 12 as event, '2014-03-19 08:30:00'::timestamp as date UNION SELECT 3 as id, 13 as event, '2014-03-19 09:00:00'::timestamp as date UNION SELECT 4 as id, 13 as event, '2014-03-19 09:30:00'::timestamp as date UNION SELECT 5 as id, 12 as event, '2014-03-19 10:00:00'::timestamp as date ) SELECT DISTINCT ON (e.id, e.date) e1.id, e.event, e2.id FROM events e LEFT JOIN events e1 ON (e1.date<=e.date AND e1.id!=e.id AND e1.event!=e.event) LEFT JOIN events e2 ON (e2.date>=e.date AND e2.id!=e.id AND e2.event!=e.event) ORDER BY e.date ASC, e.id ASC, e1.date DESC, e1.id DESC, e2.date ASC, e2.id ASC