I need to sum up the prices of accessories to the main product. There is no link between the accessories and the correspondent product, however all accessories between two product belongs to the previous product (see side note).
SQL Server 2017
Input:
| No | Order | Type | ProdNo | Price | side note
--------------------------------------
| 1 | 20213 | Product | 1320 | 200 | + 0 + 20
| 2 | 20213 | Accessory | 823 | 0 | acc. of 1320
| 3 | 20213 | Accessory | 836 | 20 | acc. of 1320
| 4 | 20213 | Product | 2680 | 300 | + 0 + 0 + 0 + 0
| 5 | 20213 | Accessory | 231 | 0 | acc. of 2680
| 6 | 20213 | Accessory | 536 | 0 | acc. of 2680
| 7 | 20213 | Accessory | 23 | 0 | acc. of 2680
| 8 | 20213 | Accessory | 361 | 0 | acc. of 2680
| 9 | 20213 | Product | 3320 | 50 | + 10 + 15
| 10 | 20213 | Accessory | 328 | 10 | acc. of 3320
| 11 | 20213 | Accessory | 369 | 15 | acc. of 3320
Output:
| No | Order | Type | ProdNo | Price |
--------------------------------------
| 1 | 20213 | Product | 1320 | 220 |
| 4 | 20213 | Product | 2680 | 300 |
| 9 | 20213 | Product | 3320 | 75 |