将数据类型更改为浮点并四舍五入为2个十进制数字

时间:2019-03-29 23:50:02

标签: sql postgresql types casting

表格:

people(id, name)
job (id, people_id, job_title, salary) 
  

目标:显示每个唯一的工作,平均总工资(浮动并四舍五入到小数点后两位)   人员和总工资(浮动并四舍五入到小数点后两位),并按最高平均工资排序。

因此,挑战在于将转换类型保持为浮点型,同时将其四舍五入到小数点后两位。

我已经到了将它四舍五入到小数点后两位的位置,但是它不是浮点数。我已经将它放到浮动的位置,但是无法将其四舍五入到小数点后两位。

我的尝试

尝试1:

SELECT 
  distinct(j.job_title) as job_title,
  to_char(AVG(j.salary)::FLOAT, 'FM999999990.00') as average_salary,
  COUNT(p.id) as total_people,
  CAST (SUM(j.salary) AS FLOAT) as total_salary
  FROM people p
    JOIN job j on p.id = j.people_id
  GROUP BY j.job_title
  ORDER BY total_salary

问题:仍然说它不是浮动的

尝试2:

SELECT 
  distinct(j.job_title) as job_title,
  CAST (AVG(j.salary) AS FLOAT) as average_salary,
  COUNT(p.id) as total_people,
  CAST (SUM(j.salary) AS FLOAT) as total_salary
  FROM people p
    JOIN job j on p.id = j.people_id
  GROUP BY j.job_title
  ORDER BY total_salary

问题:未四舍五入到小数点后两位

尝试3:

SELECT 
  distinct(j.job_title) as job_title,
  ROUND (AVG(CAST(j.salary as FLOAT)), 2)) as average_salary,
  COUNT(p.id),
  ROUND (SUM(CAST(j.salary as FLOAT)), 2)) as total_salary
  FROM people p
    JOIN job j on p.id = j.people_id
  GROUP BY j.job_title
  ORDER BY total_salary

我收到一条错误消息,说我需要添加显式强制类型,这导致我尝试编号1。

2 个答案:

答案 0 :(得分:0)

答案取决于列salary的实际数据类型。关键是Postgres中的round()不允许float(仅支持numeric类型)。

如果要处理numeric数据类型,则可以首先round(),然后强制转换为float

round(avg(salary), 2)::float

如果要处理float列,则需要在其上使用round()之前强制转换聚合函数的结果:

round(avg(salary)::numeric, 2)::float

答案 1 :(得分:0)

  

因此,挑战在于将转换类型保持为浮点型,同时将其四舍五入到小数点后两位。

如果您严格希望避免抛弃float,可以这样做:

test=# 
test=# SELECT float '12.3456'
test-#      , round(float '12.3456' * 100) / 100 AS rounded
 float8  | rounded
---------+----------
 12.3456 |    12.35

点是:round()以小数位数作为第二个参数是基于numeric的(因为浮点数本质上是不精确的)。
但是round()有一个重载的变体,它采用一个舍入到最接近的整数的参数。之前乘以100,之后乘以100。

或者克服对numeric的厌恶,并使用round(numeric, int) as provided by GMB

查询

SELECT j.job_title  -- !
     , round(AVG(j.salary) * float '100') / 100 AS average_salary  -- !
     , COUNT(p.id) AS total_people
     , SUM(j.salary)::float AS total_salary  -- short cast syntax
FROM   people p
JOIN   job    j ON p.id = j.people_id
GROUP  BY j.job_title
ORDER  BY total_salary DESC NULLS LAST -- ! "order by highest average salary"