running_sum
La función running_sum es una función de ventana que devuelve la suma acumulada de un indicador desde el inicio del rango hasta cada fila.
Requiere la cláusula ORDER BY. Se puede usar tanto la sintaxis OVER estándar como la sintaxis compacta de Crono.
Ejemplo
Sección titulada «Ejemplo»La siguiente consulta muestra las ventas de cada mes y el acumulado histórico:
select year(orders.order_date) anyo, month(orders.order_date) mes, sum(order_details.unit_price * order_details.quantity) ventas, running_sum(ventas order by anyo, mes) ventas_acumuladasfrom staging.order_detailsinner join staging.orders using order_idgroup by all;También se puede incluir PARTITION BY para reiniciar el acumulado en cada grupo:
select year(orders.order_date) anyo, month(orders.order_date) mes, sum(order_details.unit_price * order_details.quantity) ventas, running_sum(ventas partition by anyo order by mes) ventas_acumuladas_anyofrom staging.order_detailsinner join staging.orders using order_idgroup by all;