Reputation: 59
I have the following fields. Each record below is unique.
I want to get a rolling average of the field "score" over all of the preceding days for each distinct "record_type" and "something_ind"
Data:
SQL query:
select
date
,record_type
,something_ind
,avg(score) over(partition by date, record_type, something_ind order by date, record_type, something_ind rows between 6 preceding and current row as rolling_average_score
from table
group by 1,2,3
Upvotes: 0
Views: 100
Reputation: 222582
I want to get a rolling average of the field
score
over all of the preceding days for each distinctrecord_type
andsomething_ind
.
Use window functions. As I understood your question, you want:
select
t.*,
avg(score) over(
partition by record_type, something_ind
order by date
) avg_score
from mytable t
Upvotes: 1