postgresql - How to fetch next unique row from a table of postgres -
suppose table contains 2 columns "id" (type character varying(22)) , "time" (type timestamp without time zone).
now have data in table follows,
id time p001 2015-02-04 10:00:00 p002 2015-02-04 10:00:00 p003 2015-02-04 10:00:00 p004 2015-02-04 10:10:00 p005 2015-02-04 11:00:00
query first row be:
select * <tablename> order time, id limit 1;
after query next row having id value "p002"
generally offset, if there's index on time, id
can more efficient database (only noticable if have millions of rows though).
standard solution:
select * <tablename> order time, id limit 1 offset 1;
indexed (usually faster) solution:
select * <tablename> time >= '2015-02-04 10:00:00' , id > 'p001' order time, id limit 1;
Comments
Post a Comment