Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# How can I calculate average per month for each row?

Posted on 2004-04-28
Medium Priority
622 Views
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
Question by:jholton
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2

LVL 70

Accepted Solution

Scott Pletcher earned 240 total points
ID: 10940620
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 70

Expert Comment

ID: 10940653
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

Hilaire earned 160 total points
ID: 10940699
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

LVL 9

Expert Comment

ID: 10940954
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 70

Expert Comment

ID: 10941236
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

ID: 10942467
Aboslutly correct Scott.
I missed the details..
0

## Featured Post

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
###### Suggested Courses
Course of the Month9 days, 16 hours left to enroll