1
是否有可能對整數[]字段(或其他數組數組)中的所有值應用聚合(如avg(),stddev())?在Postgres的數組字段上應用聚合函數?
CREATE TABLE widget
(
measurement integer[]
);
insert into widget (measurement) values ('{1, 2, 3}');
select avg(measurement::integer[]) from widget;
ERROR: function avg(integer[]) does not exist
LINE 4: select avg(measurement::integer[]) from widget;
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
********** Error **********
ERROR: function avg(integer[]) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Character: 71
我可以解決通過分割陣列爲多行像
select avg(m)::float from (select unnest(measurement) m from widget) q;
但它是那麼優雅。
謝謝。
您可以定義一個自定義聚合,它將與數組一起使用。或者創建一個簡單的函數,將數組轉換爲單個聚合值並在此函數之上進行聚合。 –