Click here to Skip to main content
15,908,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm little confused while writing one SQL, actually the condition is little complicated. I'll try to express the condition please help me to sort this out.

In the table there are 4 fields, one is ID one is amount one is month and last one is year.
ID amount month year
1 10000 january 2013
2 5000 january 2013
3 7000 february 2013
4 9000 february 2013
5 6000 february 2013
6 5000 march 2013
7 6000 march 2013
8 1000 march 2013
9 8000 april 2013
10 7000 april 2013

Now while i need to generate january 2013 amount
SELECT SUM(amount) FROM table where month='january' and year='2013'


But now i want, if i choose february i'll get january+february sum amount
Same way, while choosing march, query returns january+february+march sum amount
while april, it'll return january+february+marcha+april sum amount

That means i just select the month and query will return sum amount till that month, so how do i solve this problem?

Thanks in advance.
Posted

1 solution

You could look at using IN clause - SELECT SUM(amount) FROM table where month in ('january','february,'march') and year='2013'.
Thus your query will dynamically have to generate the IN clause based on the month.
 
Share this answer
 
Comments
sahabiswarup 20-May-13 0:35am    
thanks a lot abhinav \m/
Abhinav S 20-May-13 0:36am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900