Solved

How can I calculate average per month for each row?

Posted on 2004-04-28
6
612 Views
Last Modified: 2011-10-03
I have a table that looks something like this:

SalesArea   |   ProductID   |   Sold       |     TotalDate
     1                     a                432               4/8/2004
     1                     b                328               4/8/3004
     1                     a                264               4/15/2004
     1                     b                128               4/15/3004
     2                     a                763               4/8/2004
     2                     b                436               4/8/3004
     2                     a                458               4/15/2004
     2                     b                274               4/15/3004

I want to write a SELECT statement that returns the info listed above along with the average SOLD per SalesArea per ProductID per Month.

So the first row returned would look like this:
SalesArea   |   ProductID   |   Sold       |     TotalDate      |    AverageForMonth
     1                     a                432               4/8/2004               348

AverageForMonth=Avg of SOLD from rows 1 and 3 since they are both the same SalesArea, ProductID and in the same Month (and year)


Thanks,
John
0
Comment
Question by:jholton
  • 3
  • 2
6 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 60 total points
Comment Utility
Please try this (*not* tested yet):

SELECT yt.*, yt2.AverageForMonth
FROM yourTable yt
INNER JOIN (
      SELECT SalesArea, ProductId, CONVERT(CHAR(7), TotalDate, 120) AS [Yr-Mth], AVG(Sold) AS AverageForMonth
      FROM yourTable yt2
      GROUP BY SalesArea, ProductId, CONVERT(CHAR(7), TotalDate, 120)
) AS yt2 ON yt.SalesArea = yt2.SalesArea AND yt.ProductId = yt2.ProductId AND
      CONVERT(CHAR(7), yt.TotalDate, 120) = yt2.[Yr-Mth]
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Seems to be working OK ... as longs as NULLs aren't an issue.  If that needs to be addressed, please let me know.
0
 
LVL 26

Assisted Solution

by:Hilaire
Hilaire earned 40 total points
Comment Utility
try this
select a.SalesArea, a.ProductID, a.Sold, a.TotalDate, avg(b.Sold) as AverageForMonth
from <YourTable> a
inner join <YourTable> b on a.SalesArea = b.SalesArea and a.ProductID = b.ProductID
and month(a.TotalDate) = month(b.TotalDate) and year(a.TotalDate) = year(b.TotalDate)
group by a.SalesArea, a.ProductID, a.Sold, a.TotalDate

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Expert Comment

by:SoftEng007
Comment Utility
Something a little faster,


select datename(month,totaldate) [Month],salesarea,productid,avg(sold)
from <tablename>
group by datename(month,totaldate),salesarea,productid
order by datename(month,totaldate),salesarea,productid
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Faster, but without the detail requested in the original post, specifically the individual dates and sold amounts.

My inner subquery could be pulled out and would give you the same result, except that it matches by both year and month, rather than just month, so more than 12 months could be viewed.
0
 
LVL 9

Expert Comment

by:SoftEng007
Comment Utility
Aboslutly correct Scott.
I missed the details..
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

743 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

16 Experts available now in Live!

Get 1:1 Help Now