SQL 将总和转换为浮点数的强制转换语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28736227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:21:11  来源:igfitidea点击:

Cast syntax to convert a sum to float

sqlpostgresqlcastingtype-conversionpostgresql-9.3

提问by MAK

Using PostgreSQL 9.3, I want to convert the calculated values to data type float.

使用 PostgreSQL 9.3,我想将计算值转换为数据类型float

My first attempt:

我的第一次尝试:

SELECT float(SUM(Seconds))/-1323 AS Averag;

Gives me this error:

给我这个错误:

syntax error at or near "SUM"
syntax error at or near "SUM"

My second attempt:

我的第二次尝试:

SELECT to_float(SUM(Seconds))/-1323 AS Averag;

Gives me this error:

给我这个错误:

 function to_float(bigint) does not exist
 function to_float(bigint) does not exist

回答by Erwin Brandstetter

There is also the shorthand cast syntax:

还有速记强制转换语法:

SELECT sum(seconds)::float / -1323 AS averag;

回答by Mureinik

You need to use the castsyntax:

您需要使用以下cast语法:

SELECT CAST (SUM(Seconds) AS FLOAT)/-1323 AS Averag;

回答by Souvik

It is not exact casting but a trick to do the job :) and works almost in any language.

这不是精确的铸造,而是完成工作的技巧:) 并且几乎可以在任何语言中使用。

SELECT SUM(Seconds)/-1323.0 AS Averag;

SELECT SUM(Seconds)/-1323.0 AS Averag;

OR

或者

SELECT SUM(Seconds)*1.0/-1323 AS Averag;

SELECT SUM(Seconds)*1.0/-1323 AS Averag;