在一个表中计算商并将其存储在另一表中 [英] Calculate a quotient in one table and store it in another table
问题描述
我有一个小型Facebook纸牌游戏,用户可以互相评分。 / p>
这些等级存储在PostgreSQL 8.4.13表 pref_rep
中作为布尔值 nice
,也可以为空:
#\d pref_rep;
表 public.pref_rep
列|类型修饰符
----------- + ----------------------------- + --- -------------------------------------------------- ------
id |字符变化(32)|不是null
作者|字符变化(32)|不是null
不错|布尔值|
条评论|字符变化(256)|
rep_id |整数|不为空默认nextval('pref_rep_rep_id_seq':: regclass)
索引:
pref_rep_pkey主键,btree(标识,作者)
检查约束:
pref_rep_check CHECK( id :: text<> author :: text)
外键约束:
pref_rep_author_fkey FOREIGN KEY(作者)参考pref_users(id)删除级联
pref_rep_id_fkey FOREIGN关键(id)参考pref_users(id)删除级联
我想将这些等级显示为用户头像上的饼图:
因此,我正在尝试以下操作-
首先选择一个商(好/好+不好)from pref_rep
:
#select id,
( count(nullif(nice,false))-count(nullif(nice,true)))/ count(nice)是pref_rep
中的评级
,其中nice不为null
按id分组;
id |评级
------------------------- + --------
DE10072 | -1
DE10086 | 0
DE10087 | 1
DE10088 | -1
DE10095 | 0
DE10097 | 1
DE10105 | 0
为什么不在这里打印0到1的浮点数? / strong>
然后由于性能原因,我试图将该商存储在 pref_users
表中想要通过每晚的cronjob来做到这一点:
#update pref_users u
设置的等级=等级
from(
select
id,
count(nullif(nice,false))-count(nullif(nice,true))/ count(nice)作为pref_rep中的等级
其中nice不为null
由id组
)s
其中u.id = s.id;
更新25419
这很快完成,但是为什么都是 pref_users
中的 rating
值设置为空吗?
评分:
选择ID,
合并(
(count(nice或null)-count(不好或null)):: float
/ count(nice)
,0)从pref_rep $ b $评级为
b按ID分组;
count
不计算空值。 true或null
将返回 true
。 false或null
将返回 null
。都将其全部转换为 float
以获得 float
的回报。
关于您的更新为什么只产生null的原因,我不知道。发布一些示例数据,以便我们可以使用它。
I have a small Facebook card game where users can rate each other.
Those ratings are stored in a PostgreSQL 8.4.13 table pref_rep
as a boolean value nice
, which can be null too:
# \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
I would like to make those ratings visible as a pie chart on user avatars:
So I am trying the following -
First select a quotient (nice / nice + not nice) from 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
Why doesn't it print a 0-to-1 floating number here?
And then I am trying to store that quotient in the pref_users
table - because of performance reasons I want to do it by a nightly 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
This completes quickly, but why are all rating
values in pref_users
are set to null?
The rating:
select id,
coalesce(
(count(nice or null) - count(not nice or null))::float
/ count(nice)
, 0) as rating
from pref_rep
group by id;
count
does not count nulls. true or null
will return true
. false or null
will return null
. It is all casted to float
to make a float
return.
As to why your update only yields nulls I don't know. Post some sample data so we can play with it.
这篇关于在一个表中计算商并将其存储在另一表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!