Change owner of all the tables and sequences

for schema in 'public' 'firstschema' 'secondschema'
do
    for tbl in `psql -h hostname -U postgres -qAt -c "select tablename from pg_tables where schemaname = '$schema';" bddname`
    do
        psql -h hostname -U postgres -c "alter table ${schema}.$tbl owner to username" bddname
    done
done
for schema in 'public' 'firstschema' 'secondschema'
do
    for ...
more ...

Renumbering the results on criteria change

SELECT depname, empno, salary, rank() OVER (PARTITION BY depname ORDER BY salary DESC) FROM empsalary;

Result:

  depname  | empno | salary | rank
-----------+-------+--------+------
 develop   |     8 |   6000 |    1
 develop   |    10 |   5200 |    2
 develop   |    11 |   5200 |    2
 develop   |     9 |   4500 |    4
 develop   |     7 |   4200 |    5
 personnel |     2 |   3900 |    1
 personnel |     5 |   3500 |    2
 sales     |     1 ...
more ...