postgres=# select * from t order by id; id | pid | rate ------+------+------ 1000 | 0 | 8 1001 | 0 | 9 1002 | 0 | 40 2000 | 1002 | 24 2001 | 1002 | 4 2002 | 1002 | 12 3000 | 2002 | 7 3001 | 2002 | 2 (8 rows)