简体   繁体   English

在 Postgres 中转换为 Float 2 dp

[英]Casting to Float 2 dp in Postgres

I'm trying to cast average_salary and total_salary as float 2 decimal places but I keep getting an error.我正在尝试将 average_salary 和 total_salary 转换为浮点数 2 位小数,但我不断收到错误消息。 Am I doing this correctly?我这样做正确吗?

Error: Test Failedexpected 2 to be a kind of Float错误:Test Failedexpected 2 to be a kind of Float

SELECT 
  j.job_title,
  SUM(j.salary)/COUNT(p.id)::float, 2 AS average_salary,
  COUNT(p.id) AS total_people,
  SUM(j.salary):: float, 2 AS total_salary
  FROM job j
    JOIN people p
    ON p.id = j.people_id
  GROUP BY j.job_title
  ORDER BY total_salary DESC
  LIMIT 100

Try the following, you are missing round() around average_salary and total_salary as you are just having , 2 without using round() .尝试以下操作,您在average_salarytotal_salary周围缺少round() ,因为您刚刚拥有, 2而没有使用round() Also if you convert SUM(j.salary) to float then it should give you the expected result.此外,如果您将SUM(j.salary)转换为浮点数,那么它应该会给您预期的结果。

SELECT 
  j.job_title,
  round(SUM(j.salary)::float/COUNT(p.id), 2) AS average_salary,
  COUNT(p.id) AS total_people,
  round(SUM(j.salary)::float, 2) AS total_salary
FROM job j
    JOIN people p
    ON p.id = j.people_id
GROUP BY j.job_title
ORDER BY total_salary DESC
LIMIT 100

You can use a floating point, but you can't set it to a certain number of decimal places.您可以使用浮点数,但不能将其设置为一定的小数位数。 The floating point types available in PostgreSQL are real or double precision . PostgreSQL 中可用的浮点类型是realdouble precision

You can either do this:您可以这样做:

expression::real

or, to get 2 decimal places, use numeric(10,2) , where 10 is the precision (the total number of digits) and 2 is the scale (the number of decimal places):或者,要获得 2 位小数,请使用numeric(10,2) ,其中10是精度(总位数), 2是小数位数(小数位数):

postgres=# SELECT (15::decimal/7)::decimal(10,2);
 numeric 
---------
    2.14
(1 row)

Note that you will have to cast one of the values in the expression as decimal , otherwise it will treat both as int , and the division will result in an integer, so casting to a decimal after that won't be accurate.请注意,您必须将表达式中的一个值转换为decimal ,否则它将两者都视为int ,并且除法将导致 integer,因此在此之后转换为decimal将不准确。

For example:例如:

postgres=# SELECT (15/7)::decimal(10,2);
 numeric 
---------
    2.00
(1 row)

This also applies to the real type, where you would have to cast at least one of the values in your expression as real to get an accurate result.这也适用于real类型,您必须将表达式中的至少一个值转换为real才能获得准确的结果。

See the documentation for more information: https://www.postgresql.org/docs/12/datatype-numeric.html有关更多信息,请参阅文档: https://www.postgresql.org/docs/12/datatype-numeric.html

Try this one:试试这个:

SELECT j.job_title,
  ROUND (AVG(j.salary), 2) ::FLOAT average_salary,
  COUNT(p.id) total_people ,
  ROUND (SUM(j.salary), 2) ::FLOAT total_salary
FROM people p
LEFT OUTER JOIN job j
ON (p.id = j.people_id)
GROUP BY j.job_title
ORDER BY average_salary DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM