CREATE TABLE pg_profiles_save (id int4, num int8);

CREATE OR REPLACE FUNCTION pg_save_profiles() RETURNS VOID AS
$$
TRUNCATE pg_profiles_save;
INSERT INTO pg_profiles_save SELECT profid, profnum FROM pg_profiles;
$$
LANGUAGE sql;

CREATE OR REPLACE VIEW pg_diff_profiles AS
  SELECT profid, profname,
         (100 * profnum / sum(profnum) OVER ())::numeric(5, 2) AS percent
    FROM (SELECT profid, profname, profnum - num AS profnum
            FROM pg_profiles, pg_profiles_save
           WHERE profid = id AND profnum - num > 0) AS t
   ORDER BY profnum DESC;
