sql - How to get second row in PostgreSQL? -
i have next table:
column_name data_type pk nullable inc_id bigint yes no dt_cr timestamp no no email varchar(255) no yes email column not unique, should use group email.
question: how can table minimal date, next row after minimal date , email related dates?
i read first_value , nth_value functions, code listed below, seems doesn't work correct.
select j1.email, j2.first_date, j2.second_date ( select email orders group email) j1 left outer join ( select email, first_value(dt_cr) on (order dt_cr rows between unbounded preceding , unbounded following) first_date, nth_value(dt_cr, 2) on (order dt_cr rows between unbounded preceding , unbounded following) second_date orders) j2 on (j1.email=j2.email); any ideas?
--edited maybe can done rank() function..
this query uses with construction works similar sub-queries. investigate query explain before use in production because may slow on big tables:
with orders ( select email , first_value(dt_cr) on wnd1 min_date , nth_value(dt_cr, 2) on wnd1 second_date orders window wnd1 ( partition email order email, dt_cr) ) select distinct * orders second_date not null;
Comments
Post a Comment