POSTGRES select n equally distributed rows by time over millions of records
问题 I have a table with columns id,filter1,filter2,time,value which contains millions of records. I want to fetch n equally distributed rows between two timestamps. If the number of records between timestamps is less than n I want to fetch all the records. My current query looks like below, assuming n=200 SELECT s.* FROM ( SELECT t.time, t.value, ROW_NUMBER() OVER(ORDER BY t.time) as rnk, COUNT(*) OVER() as total_cnt FROM table_name t WHERE t.filter1='filter_value' and t.filter2='another_value'