我使用pgAdmin 4.。并且我试图弄清楚如何舍入column Rental_period列,该列是从扣除2个时间戳列中提取的,现在由双精度数据类型组成。我想将其四舍五入到小数点后两位。这是返回错误的脚本:
SELECT customer_id, ROUND (AVG (extract (day from (return_date - rental_date))), 2) AS rental_period
FROM rental
GROUP BY customer_id;
错误提示:
错误:“ AS”处或附近的语法错误
第1行:... xtract(从(返回日期-出租日期)开始的天,2) 租金_... ^ SQL状态:42601
字符:84
这是一个运行良好的脚本,但返回的小数位数太多:
SELECT customer_id, AVG (extract (day from (return_date - rental_date))) AS rental_period
FROM rental
GROUP BY customer_id;
谢谢)
答案 0 :(得分:0)
尝试转换为十进制类型
AVG cast((extract (day from (return_date - rental_date))) as decimal(10,2)) AS rental_period
答案 1 :(得分:0)
我无法重现您所显示的错误,您的实际代码必须存在语法问题。
但是,这失败了(有另一个错误):
@Service
public class NumberCacheScheduler {
@Autowired
NumberService numberService;
@PostConstruct
public void init() {
update();
}
public void update() {
for (long l=1l; i <=10,l++) {
numberService.square(l);
}
}
}
原因是不存在以SELECT round(
EXTRACT(day FROM (DATE '2019-06-30')),
2
);
ERROR: function round(double precision, integer) does not exist
LINE 1: SELECT round(EXTRACT(day FROM (DATE '2019-06-30')), 2);
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
作为第一个参数的两个参数round
的功能:
double precision
您将必须为\df round
List of functions
Schema | Name | Result data type | Argument data types | Type
------------+-------+------------------+---------------------+------
pg_catalog | round | double precision | double precision | func
pg_catalog | round | numeric | numeric | func
pg_catalog | round | numeric | numeric, integer | func
(3 rows)
添加一个显式类型强制转换:
numeric
答案 2 :(得分:0)
减去两个日期将返回天数差异。那已经是numeric
了,所以不需要使用extract
:
SELECT customer_id
, ROUND(AVG(return_date - rental_date), 2) AS rental_period
FROM rentals
GROUP BY
customer_id