473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Year to Month Sum

3
Hello,
My problem is to summarize
Year to Month Orders
For example I have a table like this :
[Date] ------------------------------------[Orders]
2007-01-31 00:00:00.000------------- 2
2007-02-28 00:00:00.000------------- 8
2007-03-31 00:00:00.000------------- 7
2007-04-30 00:00:00.000------------- 1

and my need is to sum each month's Orders to next one
to Look like this :

2007-01-31 00:00:00.000------------- 2
2007-02-28 00:00:00.000------------- 10
2007-03-31 00:00:00.000------------- 17
2007-04-30 00:00:00.000------------- 18

Thanks in advance .
Mar 17 '08 #1
4 1507
deepuv04
227 Expert 100+
Hello,
My problem is to summarize
Year to Month Orders
For example I have a table like this :
[Date] ------------------------------------[Orders]
2007-01-31 00:00:00.000------------- 2
2007-02-28 00:00:00.000------------- 8
2007-03-31 00:00:00.000------------- 7
2007-04-30 00:00:00.000------------- 1

and my need is to sum each month's Orders to next one
to Look like this :

2007-01-31 00:00:00.000------------- 2
2007-02-28 00:00:00.000------------- 10
2007-03-31 00:00:00.000------------- 17
2007-04-30 00:00:00.000------------- 18

Thanks in advance .
Hi,
Use the following query

Expand|Select|Wrap|Line Numbers
  1. select CurMon.Date, isnull(CurMon.Orders,0) + isnull(PrevMon.Orders,0)
  2. from   YearSum as CurMon
  3.        LEFT OUTER JOIN YearSum as NextMon
  4.        ON Month(CurMon.Date)  = Month(NextMon.Date) - 1
  5.        LEFT OUTER JOIN YearSum as PrevMon
  6.        ON Month(CurMon.Date)  = Month(PrevMon.Date) + 1
  7.  
Mar 17 '08 #2
Ph0b0s
3
Hi,
Use the following query

Expand|Select|Wrap|Line Numbers
  1. select CurMon.Date, isnull(CurMon.Orders,0) + isnull(PrevMon.Orders,0)
  2. from   YearSum as CurMon
  3.        LEFT OUTER JOIN YearSum as NextMon
  4.        ON Month(CurMon.Date)  = Month(NextMon.Date) - 1
  5.        LEFT OUTER JOIN YearSum as PrevMon
  6.        ON Month(CurMon.Date)  = Month(PrevMon.Date) + 1
  7.  
thanks 4 your answer it was exactly what i asked.
and it was not what i wonted in the first place.... Sorry
I want every month to summarize the priv month sum :-| .
Ex: 1 month (1)
2nd month (3)
the record of the 2nd month should be (4) ,
3rd month (5)
the record of the 3nd month should be (9)etc..

thanks
Mar 17 '08 #3
deepuv04
227 Expert 100+
thanks 4 your answer it was exactly what i asked.
and it was not what i wonted in the first place.... Sorry
I want every month to summarize the priv month sum :-| .
Ex: 1 month (1)
2nd month (3)
the record of the 2nd month should be (4) ,
3rd month (5)
the record of the 3nd month should be (9)etc..

thanks
try the following query
use appropriate table names and column names

Expand|Select|Wrap|Line Numbers
  1. SELECT Date1,sum(Orders2)
  2. from(
  3.     SELECT DISTINCT * --,DATEDIFF(MM,T2.DATE2,T1.DATE1)
  4.     FROM 
  5.         ( SELECT Date AS DATE1,Orders  AS ORDERS1
  6.           FROM Table_Name ) AS T1 ,
  7.         ( SELECT Date AS DATE2,Orders AS ORDERS2
  8.           FROM Table_Name ) AS T2 
  9.     WHERE  DATEDIFF(MM,T2.DATE2,T1.DATE1) >= 0
  10.     ) as s
  11. group by date1
  12.  
Mar 17 '08 #4
Ph0b0s
3
try the following query
use appropriate table names and column names

Expand|Select|Wrap|Line Numbers
  1. SELECT Date1,sum(Orders2)
  2. from(
  3.     SELECT DISTINCT * --,DATEDIFF(MM,T2.DATE2,T1.DATE1)
  4.     FROM 
  5.         ( SELECT Date AS DATE1,Orders  AS ORDERS1
  6.           FROM Table_Name ) AS T1 ,
  7.         ( SELECT Date AS DATE2,Orders AS ORDERS2
  8.           FROM Table_Name ) AS T2 
  9.     WHERE  DATEDIFF(MM,T2.DATE2,T1.DATE1) >= 0
  10.     ) as s
  11. group by date1
  12.  
thank you very much!
Mar 17 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ken Fine | last post by:
I want my application to maintain a directory tree based on months and years, e.g.: 2004 January file file file February file
4
by: Ken Fine | last post by:
Using ASP, I want to display a series of records grouped by nested years and months, e.g.: 2004 December Record1 Record2 Record3 November Record4
8
by: Zero.NULL | last post by:
Hi, We are using Month-year tables to keep the history of long transaction of our application. For example: We capture the details of a certain action in table...
7
by: Fendi Baba | last post by:
The function is called from opencalendar(targetfield). Thanks for any hints on what could be the problem. .............................................................. var...
20
by: Laguna | last post by:
Hi Gurus, I want to find the expiration date of stock options (3rd Friday of the month) for an any give month and year. I have tried a few tricks with the functions provided by the built-in...
19
by: Ricardo Perez Lopez | last post by:
Hello everyone: I'm a PostgreSQL newbie, working now with dates, times, timestamps and intervals. I have three questions about the above: FIRST: --------
1
by: Helge's | last post by:
I have an orderdatabase with DATE and SALES as one of the fields in a table. How would the SQL look like if I want a table with MONTH, 2006, 2005, DIFFERENCE. I think when I have the field DATE I...
2
by: owz | last post by:
Ok, I am making a program (java class file) 2 work out if a date entered is valid or invalid for the day, month year, and for leap years.. dd/mm/yyyy . I seem 2 have gotten it 2 validate the year...
5
by: Kasrav | last post by:
I have another problem hopeful am not bothering you guys too much if u can help that would be wonderful. I have this code here def year2(): print'This program validates days and months of the...
25
by: rekhasc | last post by:
hi...... Its very urgent.............. how can i take only the year and month part and it should save in the database in the month and year field separately......... when i enter the date in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.