?
Solved

Average number of records for past 3 fiscal years grouped by fiscal month

Posted on 2014-01-07
4
Medium Priority
?
380 Views
Last Modified: 2014-01-08
Hi Experts:

I need to  count a certain type of order for the past 3 fiscal years breaking the data out by fiscal month.  So the first number would be the average the count of records for Jan 2013, Jan 2012, Jan 2011, and so on.  The database has a table called ORDERS and one called ORDER_DATE

Here is what I came up with for the full set of data:
select count(*) as myCount,
        od.fiscal_month,
        od.fiscal_year
from orders o
INNER JOIN order_date od ON o.datesubmitted = od.caldate 
where od.fiscalyear in (2013,2012,2011)
and o.order_type='Y'
group by od.fiscal_year,od.fiscal_month
order by od.fiscal_month

Open in new window

I get this:
      cnt  fm          fy
        72 01          2011      
        84 01          2012      
        96 01          2013      
        72 02          2011      
        85 02          2012      
        90 02          2013  
and so on for each fiscal month for each of the three fiscal years.

What I really want is the average for each month like this:
   avg  fm  (72+94+96)/3
  84    01  
 82.3  02  (72+85+90)/3

and so on.

I can get the average of ALL, but I want it broken out by fiscal month over the past 3 fiscal years.  

Any ideas?

Thanks!
0
Comment
Question by:Breadbaker
  • 2
4 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 39763042
select avg(cnt), fm from
(select count(*) as myCount,
        od.fiscal_month,
        od.fiscal_year
from orders o
INNER JOIN order_date od ON o.datesubmitted = od.caldate
where od.fiscalyear in (2013,2012,2011)
and o.order_type='Y'
group by od.fiscal_year,od.fiscal_month)
group by fm
order by fm
0
 
LVL 32

Expert Comment

by:awking00
ID: 39765506
select avg(cnt) avg, fm from
(select count(*) over (partition by od.fiscal_month order by od.fiscal_year) cnt, fiscal_month fm
 from orders o inner join order_date od on o.datesubmitted = od.caldate
 where od.fiscal_year between 2011 and 2013
 and o.order_type = 'Y')
0
 

Author Closing Comment

by:Breadbaker
ID: 39765524
Thanks a bunch.. I was very close, but this cleared it up!
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39765536
ID: 39765506

is illegal syntax (no group by) and the analytic will produce the incorrect results
you need to maintain the year within the groups/partitions
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

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