我有以下查询,我试图对列进行算术运算。我已经使用了基于postgres文档的正确的强制转换但是(http://www.postgresql.org/docs/8.3/static/datatype-numeric.html)但是我的bigint列仍然会删除结果并避免结果集上的小数点。
select * from (
select (degree_one + degree_two) as degree_easy,
degree_three as degree_hard,
(((degree_one + degree_two)/(degree_one + degree_two + degree_three))::decimal) as easy_percent, ((degree_three/(degree_one + degree_two + degree_three))::decimal) as hard_percent from recommendation_degree
) as dc
where dc.degree_easy >= 4 and dc.degree_hard >= 4
我在这里做错了什么?除了十进制,我已经尝试了float,但它们都给出了相同的结果。
答案 0 :(得分:1)
您可以尝试在分区中转换被除数和除数以强制计算在十进制值上进行,否则我认为您最终会进行整数除法(然后将其转换为十进制)并且结果将不对。
试试这个:
select * from (
select (degree_one + degree_two) as degree_easy,
degree_three as degree_hard,
(degree_one::decimal + degree_two::decimal)/(degree_one::decimal + degree_two::decimal + degree_three::decimal) as easy_percent,
degree_three::decimal/(degree_one::decimal + degree_two::decimal + degree_three::decimal) as hard_percent
from recommendation_degree
) as dc
where dc.degree_easy >= 4 and dc.degree_hard >= 4
可能没有必要同时使用被除数和除数,只有在我只考虑除数时它才有效。