【发布时间】:2013-03-16 16:47:24
【问题描述】:
在表my_obj中有两个整数字段:
(value_a integer, value_b integer);
我尝试计算value_a = value_b 的次数,我想用百分比来表示这个比率。
这是我尝试过的代码:
select sum(case when o.value_a = o.value_b then 1 else 0 end) as nb_ok,
sum(case when o.value_a != o.value_b then 1 else 0 end) as nb_not_ok,
compute_percent(nb_ok,nb_not_ok)
from my_obj as o
group by o.property_name;
compute_percent 是一个简单的存储过程 (a * 100) / (a + b)
但 PostgreSQL 抱怨 nb_ok 列不存在。
你会如何正确地做到这一点?
我在 Ubuntu 12.04 中使用 PostgreSQL 9.1。
【问题讨论】:
-
But postgresql complains that the column nook doesn't exist.?请解决您的问题,任何地方都没有nook。在谈论错误消息时,请按原样将其放在您的问题中。复制/粘贴。 -
考虑我回答的最后一段,并点击手册的链接,了解为什么
nbok以小写形式出现。 -
SQL 关键字的大写完全是可选的,只是个人喜好问题。但标识符的小写字母不是。
标签: sql postgresql count aggregate-functions postgresql-9.1