Money数据类型的平均值

mrg*_*mrg 1 regex sql 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=>
Run Code Online (Sandbox Code Playgroud)

现在,我想取价格的平均值。因此尝试了以下方法。但是会产生错误。

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=>
Run Code Online (Sandbox Code Playgroud)

因此,以货币数据类型获取价格列平均值的方法是什么。

Viv*_* S. 6

SELECT AVG(price::numeric) FROM pc;
Run Code Online (Sandbox Code Playgroud)

要么

根据PostgreSQL 8 .4的货币类型

select avg(regexp_replace(price::text, '[$,]', '', 'g')::numeric) from pc
Run Code Online (Sandbox Code Playgroud)