Hi - I have below two tables:
Table1:
store | date | revenue |
A | 10-01-2013 | 10 |
A | 11-01-2013 | 100 |
A | 12-01-2013 | 12 |
B | 10-01-2013 | 33 |
B | 11-01-2013 | 56 |
B | 12-01-2013 | 56 |
Table2:
Store | month | year | Status |
A | 10 | 2013 | Managed |
A | 11 | 2013 | Tracked |
A | 12 | 2013 | Managed |
B | 10 | 2013 | Managed |
B | 11 | 2013 | Tracked |
B | 12 | 2013 | Managed |
My requirement is, I need to sum the revenue in Table1 for the stores whose status is 'Managed' in Table2. Per say, Store A is Managed during 10/2013 and 12/2013, so I need the revenue 22(10+22). Since it was Tracked during 11/2013 and hence it should not be counted while summing.
My final result should be:
Store Revenue
== ======
A 22
B 89
Could you please tell me how can I achieve it using SQL query.
thanks