0

I have 2 tables as

Table 1

-------------------------------
 ID | Name | Bottles | Balance
-------------------------------
  1 | ABC  |    2    |  200
  2 | DEF  |    5    |  500
  3 | GHI  |    3    |  100

Table 2

-----------------------------
  CID | Add_Bottles | Amount  
-----------------------------
   2  |      2      |  100
   1  |      1      |  50
   2  |      3      |  150
   1  |      1      |  50
   3  |      2      |  100

What I want to get is First get Bottles and Balance from table1 and then add bottles and balance to table2 rows and continue. I mean, for example, the result for CID 2 should be like this

--------------------------------------------------
 CID  | Bottles | ttlBottles | Amount |   Balance  
--------------------------------------------------
  2   |    0    |     5      |    0   |    500  <-- First Row from table1          
  2   |    2    |     7      |   100  |    600  <-- Now adding bottles and balance from tabl2
  2   |    3    |     10     |   150  |    750

I can't get an idea that what should be my query to do so. What would you suggest?

Airy
  • 5,484
  • 7
  • 53
  • 78
  • 1
    Here's what I suggest: http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query – Strawberry Sep 09 '16 at 15:30
  • This is called cumulative sum. There are quite a few topics around this in SO. – Shadow Sep 09 '16 at 15:47
  • @Shadow Ok let me see about cumulative sum. I Didn't know about this – Airy Sep 09 '16 at 15:48

0 Answers0