使用sql,我如何计算给定天数内的滚动平均值,以及这些天数内未指定数量的记录?

iqjalb3h  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(401)

我有以下字段。下面的每条记录都是唯一的。
我想得到每个不同的“记录类型”和“某物类型”的前几天字段“得分”的滚动平均值
数据:

期望输出:

sql查询:

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
hpcdzsge

hpcdzsge1#

我想得到这个领域的滚动平均值 score 在过去的每一天 record_type 以及 something_ind .
使用窗口函数。我理解你的问题,你想:

select
    t.*,
    avg(score) over(
        partition by record_type, something_ind 
        order by date
    ) avg_score
from mytable t

相关问题