我需要编写一个查询,给出 1 月 31 日每天处理的总交易金额的滚动 3 天平均值
我使用了下面的查询,但我在检索信息时遇到问题,因为它不断给出此错误:
查询错误:错误:列“sum_pday.sumamount”必须出现在 GROUP BY 子句中或在聚合函数中使用
select
Sum_pDay.day
, Sum_pDay.Month
, Sum_pDay.Year
, avg (Sum_pDay.SumAmount) over (order by Sum_pDay.day rows between 2 preceding and current row) as Avg
from (
select
extract (year from transaction_time) as Year
, extract (month from transaction_time) as Month
, extract (day from transaction_time) as day
, sum (transaction_amount) as SumAmount
from
transactions
group by Year, Month, day
order by 3) as Sum_pDay
group by 1,2,3
错误消息告诉您需要按该字段进行分组,因此这可能是解决方案:
select
Sum_pDay.day
, Sum_pDay.Month
, Sum_pDay.Year
, avg (Sum_pDay.SumAmount) over (order by Sum_pDay.day rows between 2 preceding and current row) as Avg
from (
select
extract (year from transaction_time) as Year
, extract (month from transaction_time) as Month
, extract (day from transaction_time) as day
, sum (transaction_amount) as SumAmount
from
transactions
group by Year, Month, day
order by 3) as Sum_pDay
group by 1,2,3, 4
如果有效请告诉我。
当您使用
GROUP BY
时,错误消息很清楚,每列必须位于分组依据中或具有聚合函数。
在您的情况下,您可以对组中已有的金额进行求和
更好的是变体 2,因为你真的不需要聚合
CREATE TABLe transactions (transaction_time date,transaction_amount decimal(10,2))
CREATE TABLE
select
Sum_pDay.day
, Sum_pDay.Month
, Sum_pDay.Year
, avg (SUM(Sum_pDay.SumAmount)) over (order by Sum_pDay.day rows between 2 preceding and current row) as Avg
from (
select
extract (year from transaction_time) as Year
, extract (month from transaction_time) as Month
, extract (day from transaction_time) as day
, sum (transaction_amount) as SumAmount
from
transactions
group by Year, Month, day
order by 3) as Sum_pDay
group by 1,2,3
日 | 月 | 年 | 平均 |
---|
SELECT 0
select
Sum_pDay.day
, Sum_pDay.Month
, Sum_pDay.Year
, avg (Sum_pDay.SumAmount) over (order by Sum_pDay.day rows between 2 preceding and current row) as Avg
from (
select
extract (year from transaction_time) as Year
, extract (month from transaction_time) as Month
, extract (day from transaction_time) as day
, sum (transaction_amount) as SumAmount
from
transactions
group by Year, Month, day
order by 3) as Sum_pDay
日 | 月 | 年 | 平均 |
---|
SELECT 0