PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Come calcolare una somma cumulativa dal basso verso l'alto?

Prova questo

SELECT 100+ Sum(CASE WHEN name = 'a' THEN qty ELSE 0 END)OVER(ORDER BY itemorder DESC) as modifyed_sum,
       qty,
       name,
       itemorder
FROM   Yourtable
ORDER  BY itemorder ASC

Un altro modo

SELECT 100 + (SELECT Sum(CASE WHEN b.name = 'a' THEN b.qty ELSE 0 END)
              FROM   yourtable  b
              WHERE  a.itemorder <= b.itemorder),
       qty,
       name,
       itemorder
FROM   yourtable a
ORDER  BY itemorder ASC