我有一个小型的 Facebook 纸牌游戏,用户可以在其中互相评分。
这些评级pref_rep作为布尔值存储在 PostgreSQL 8.4.13 表中nice,也可以为空:
# \d pref_rep;
                                       Table "public.pref_rep"
  Column   |            Type             |                         Modifiers
-----------+-----------------------------+-----------------------------------------------------------
 id        | character varying(32)       | not null
 author    | character varying(32)       | not null
 nice      | boolean                     |
 comment   | character varying(256)      |
 rep_id    | integer                     | not null default nextval('pref_rep_rep_id_seq'::regclass)
Indexes:
    "pref_rep_pkey" PRIMARY KEY, btree (id, author)
Check constraints:
    "pref_rep_check" CHECK (id::text <> author::text)
Foreign-key constraints:
    "pref_rep_author_fkey" FOREIGN KEY (author) REFERENCES pref_users(id) ON DELETE CASCADE
    "pref_rep_id_fkey" FOREIGN KEY (id) REFERENCES pref_users(id) ON DELETE CASCADE
我想让这些评分在用户头像上显示为饼图:

所以我正在尝试以下 -
首先从以下选择一个商(nice / nice + not nice)pref_rep:
# select id,
    (count(nullif(nice, false)) - count(nullif(nice, true))) / count(nice) as rating
    from pref_rep
    where nice is not null
    group by id;
           id            | rating
-------------------------+--------
 DE10072                 |     -1
 DE10086                 |      0
 DE10087                 |      1
 DE10088                 |     -1
 DE10095                 |      0
 DE10097                 |      1
 DE10105                 |      0
为什么它不在这里打印一个 0 到 1 的浮点数?
然后我试图将该商存储在pref_users表中 - 由于性能原因,我想通过每晚的 cronjob 来完成它:
# update pref_users u
set rating = s.rating
from (
        select
        id,
        count(nullif(nice, false)) - count(nullif(nice, true)) / count(nice) as rating
        from pref_rep
        where nice is not null
        group by id
) s
where u.id = s.id;
UPDATE 25419
这很快就完成了,但是为什么所有的rating值pref_users都设置为null?