SQL limit by group by SQL limit by group by postgresql postgresql

SQL limit by group by


WITH CTE AS(SELECT id,lat,lon,trajectory_id,time,ROW_NUMBER() OVER(PARTITION BY trajectory_id ORDER BY time) rnFROM t)SELECT id,lat,lon,trajectory_id,time FROM CTE WHERE rn=1


Use ROW_NUMBER() per trajectory_id in order to only retrieve the first row per trajectory_id.

select id, lat, lon, trajectory_id, timefrom(  select     mytable.*,     row_number() over (partition by trajectory_id order by time) as rn  from mytable) numberedwhere rn = 1;