?
Solved

Need SQL staement for AVERAGE of pevious 3 months data

Posted on 2006-11-22
2
Medium Priority
?
240 Views
Last Modified: 2006-11-22
Hi
 have a sales table where I'd like to extract sales figure for the month, but also sales for figures for the AVERAGE of the 3 months prior to the month

eg;

MONTH      SALES        AVG. PREV 3 MONTH SALES
JAN 2006    50             n/a
FEB 2006    80             n/a
MAR 2006   30             n/a
APR 2006    70             53.3


thanks a lot for any help!
Fergal
0
Comment
Question by:fjkilken
  • 2
2 Comments
 
LVL 28

Expert Comment

by:imran_fast
ID: 17995512


select month, sales, (select avg(b.sales) from yourtable b where month(b.date) between month(a.date) -3 and month(a.date) -1 and year(b.date) = year(a.date)) averagefor3month
from yourtable
0
 
LVL 28

Accepted Solution

by:
imran_fast earned 1500 total points
ID: 17995516
correction

select a.month, a.sales, (select avg(b.sales) from yourtable b where month(b.date) between month(a.date) -3 and month(a.date) -1 and year(b.date) = year(a.date)) averagefor3month
from yourtable A
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question