以下是“pc”表,其中包含有关 pc 的详细信息。
user=> SELECT * FROM pc;
code | model | speed | ram | hd | cd | price
------+----------+-------+------+------+-----+---------
101 | Imac | 2000 | 4096 | 500 | 16x | ₹550.00
102 | G450 | 1500 | 2048 | 500 | 8x | ₹450.00
(2 rows)
user=>
现在我想取价格的平均值。于是尝试了下面的方法。但它会产生错误。
user=> SELECT AVG(price) FROM pc;
ERROR: function avg(money) does not exist
LINE 1: SELECT AVG(price) FROM pc;
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
user=>
那么,如何获取货币数据类型的价格列的平均值。
最佳答案
SELECT AVG(price::numeric) FROM pc;
或
根据 PostgreSQL 8 .4 的 Monetary Types
select avg(regexp_replace(price::text, '[$,]', '', 'g')::numeric) from pc
关于sql - Money 数据类型的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34390708/