sql >> Base de Datos >  >> RDS >> Oracle

Oracle Query para acumular CANTIDAD por año:solo los últimos 3 años

Un método utiliza dos niveles de funciones analíticas:

select t.*, max(running_avg_3) over (partition by item_id)
from (select t.*,
             avg(qty) over (partition by item_id order by year desc
                            rows between current row and 2 following
                           ) as running_avg_3
      from t
     ) t