Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Build Sql Query We have a Sql Table Having Three Columns Viz Name_of_ffice , Month , Amount


Name_Of_Office	Month	Amount
DivisionBhopal	04	125
DivisionBhopal	05	50
DivisionBhopal	06	100
DivisionBhopal	10	125
DivisionSagar	04	600
DivisionSagar	05	520
DivisionSagar	06	400
DivisionSagar	10	100


FINANCIAL Year Month Should We Taken For Calculation . By the Formula Always Start sum From April to Selected Month Suppose User will Select Month June (from dropdown) Calculation Should be Performed Like this

Name_Of_Office	Sum Upto Previous Month	Present Month 	Total Amount
DivisionBhopa	April + May	June	April + May+ June
DivisionSagar	April + May	June	April + May+ June


Here we will not add oct Amount because Selected Month is June we want data till june

For June Month Data Should look like
Name_Of_Office	Sum Upto Previous Month	Present Month	Total Amount
DivisionBhopal	175		100	275
DivisionSagar	1120	400	1520


For October Month Data Should look like (If User Select December)
Name_Of_Office	Sum Upto Previous Month	Present Month	Total Amount
DivisionBhopal	275		125	400
Divisionsagar	1520	100	1620
Posted
Updated 25-Apr-15 22:32pm
v4
Comments
Deepu S Nair 26-Apr-15 1:52am    
Homework?
Tomas Takac 26-Apr-15 2:19am    
You need to format the text properly. Use the <pre> tag to preserve spaces.
OriginalGriff 26-Apr-15 3:32am    
There are no spaces to preserve - that's exactly as he wants it :X
Maciej Los 26-Apr-15 4:34am    
This is not a question at all! This is a request.
What have you tried till now? Where are you stuck?

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Comments
Member9927612 1-May-15 6:43am    
i have try this but not working
select office,
sum(amount)as total,
sum(case when month = @passedmonth then amount else 0 end) as present,
sum(case when month < @passedmonth then amount else 0 end) as prev
from table
where month >= 4 and month < @passedmonth
group by office
OriginalGriff 1-May-15 6:59am    
That's not a helpful error description, is it? :laugh:
"not working" could mean anything!

So tell us what it did that you didn't expect, or didn't do that you did...
Remember that we can't see your screen, access your HDD, or read your mind
All you need is called: running sum[^] or running total[^]

How to achieve that? Please, read this: Calculating simple running totals in SQL Server[^]
 
Share this answer
 

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