I need to write a Query that gives me January 31's rolling 3 day average of total transaction amount processed per day
I used the query below, but I'm having problem to retrieve information, because it keeps giving this error:
Query Error: error: column "sum_pday.sumamount" must appear in the GROUP BY clause or be used in an aggregate function
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
The error message is clear when you use a
GROUP BYevery column must be in the group by or have a aggregation function.In your case you can SUM the amount as it is already in the group by
better is variant 2 as you really don't need the aggregation
fiddle