sql - Money 数据类型的平均值

标签 sql regex postgresql average postgresql-8.4

以下是“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/

相关文章:

sql - 如何重命名 SQL 表列名称而不破坏内容

python - 从 SQLAlchemy+Postgres 中的 IntegrityError 中获取约束名称

json - postgresql文本到json的转换

SQLITE 自定义数据类型?

sql - 简单的 SQL 报告服务问题

mysql - 当查询次数大于某个数字时返回查询?

php - 获取非数字字符,然后在文本 block 的每一行上编号

regex - Emacs,删除所有不匹配的行?

ruby-on-rails - 在 Ruby 中解析日期

mysql - Postgresql 中 ELT() 函数的替代方法是什么?