SQLTeam.com | Weblogs | Forums

Rolling 6 months data summation for each Month


#1

Hi Team,

I have a data like below. I need to write a query to get the result for rolling 6 months on each month. Please find mentioned expected result in the attached screenshot.

Can any one help me how to write the logic for rolling 6 months.

I am very new to SQL, hope someone will help me to get this requirement.

And also please mention any practice links where i can do real time scenario to improve the SQL query writing with real time requirements.


#2

Couple of comments about your post:

  1. If you only have the "Month of Order Date", in your data, that is not a good thing For two reasons a) if you end up with data for more than one year, or if you end up with data starting in months other than January, it can cause problems.
  2. It is hard to sort on names of months (which is required to accomplish what you want).

What I would suggest is to use DATE data type (with perhaps the first month of the date).

All that said, this page shows you exactly how to write the query.

One other thing: In that page, they create a sample table to show you what to do . That is much preferable to posting screen-shots.

If you still have problems after trying the approach on that page, post the script to create the table and I am sure someone will respond.