Solved

Need SQL staement for AVERAGE of pevious 3 months data

Posted on 2006-11-22
2
231 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 500 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now