I have a table Orders with order_date and order_total. Is it possible to find out how much was averaged ordered per month?
=== ORDER_DATE === ORDER_TOTAL
--- 7-JAN-10 --- 5271
--- 12-JAN-10 --- 3646
--- 15-JAN-10 --- 310
--- 5-FEB-10 --- 71173
--- 12-FEB-10 --- 45175
--- 25-FEB-10 --- 126
In this case it would be:
JAN - 3075.66667 // calculation: (5271 + 3646 + 310) / 3
FEB - 38824.6667 // calculation: (71173 + 45175 + 126) / 3
You’ll want to reduce each date to its month. The
TRUNC()function below will return the first day of the month for each date. AVG() is an aggregate function, so you’ll want toGROUP BYthe month.Another way to accomplish this with analytic/window functions (this way you can return all the order data along with the average per month) would be as follows:
The OP asked the following:
I had suggested using
TO_CHAR()with a mask of ‘MON-YY’ in this case but this meant that he could no longer sort properly on that column since it was now a string and not a date. One way to handle this would be as follows: