2014-11-28 12 views
5
+--------+---------+-------+--------+ 
| billID | orderId | price | date | 
+--------+---------+-------+--------+ 
|  1 |  1 | 100 | 1.3.12 | 
|  2 |  1 | 230 | 1.4.12 | 
|  3 |  1 | 300 | 1.5.12 | 
|  4 |  2 | 1000 | 1.3.12 | 
|  5 |  2 | 160 | 1.4.12 | 
|  6 |  3 | 400 | 1.3.12 | 
+--------+---------+-------+--------+ 

Voglio creare una vista che abbia una colonna che sommi tutti i prezzi abbiano lo stesso orderID ma con data precedente alla data di righe. Come questo:Come posso selezionare una colonna per sommare il prezzo totale dalla data precedente

+--------+---------+-------+--------+--------------+ 
| billID | orderId | price | date | add-on price | 
+--------+---------+-------+--------+--------------+ 
|  |   |  |  |    | 
|  1 |  1 | 100 | 1.3.12 |   100 | 
|  2 |  1 | 230 | 1.4.12 |   330 | 
|  3 |  1 | 300 | 1.5.12 |   630 | 
|  4 |  2 | 1000 | 1.3.12 |   1000 | 
|  5 |  2 | 160 | 1.4.12 |   1160 | 
|  6 |  3 | 400 | 1.3.12 |   400 | 
+--------+---------+-------+--------+--------------+ 

risposta

2

È possibile all'utente una subquery correlata per questo:

select t.*, 
     (select sum(t2.price) 
     from table t2 
     where t2.orderId = t.orderId and t2.date <= t.date 
     ) as CumulativePrice 
from table t; 
Problemi correlati