Postgresql:尝试获得过去10天的平均值

DUn*_*wn1 5 mysql sql database postgresql

Select avg(last_10_count) AS last_10_avg
(Select count(*)
from dim_user
where effective_date ::date > current_date -10
group by effective_date ::date) AS last_10_count
Run Code Online (Sandbox Code Playgroud)

当我只运行内联查询时,我得到了所需的结果,但是当我运行整个查询时,它会抛出以下错误:

ERROR: function avg(record) does not exist
LINE 1: Select avg(last_10_count) AS last_10_avg
HINT: NO function matches the given name and arguement types.
      You might need to add explicit type casts.
************Error***************
ERROR: function avg(record)  does not exit
SQL state: 42883
Run Code Online (Sandbox Code Playgroud)

小智 8

试试这个吧

Select avg(last_10_count) AS last_10_avg from 
(Select count(*) as last_10_count
from dim_user
where effective_date::date  > current_date -10
group by effective_date :: date) Z
Run Code Online (Sandbox Code Playgroud)

  • Z 不影响这里的查询。它是给子查询的别名,因为 sql 需要它才能临时存储子查询的结果。更准确地说,没有别名的子查询是无效的,就像没有名称的表一样。 (2认同)