Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

getting daily average of sales from monthly report

034becf1-46d1-48f2-aa64-3bb226e56049Oct 9 2014 — edited Oct 9 2014

Hi, need a quick help here
I have this small project, I have this report that have the total of order along with the date of the order

SELECT  sf.ORDER_QNT, dd.ACTUAL_DATE, dd.MONTH_NUMBER

FROM    sales_fact sf,

        date_dim dd

WHERE   dd.date_id = sf.date_id

AND dd.MONTH_NUMBER = 1;

ORDER_QNT  ACTUAL_DATE MONTH_NUMBER

      1100            05/01/13            1

       100             05/01/13            1

       140             06/01/13            1

       110             07/01/13            1

       200             08/01/13            1

       500             08/01/13            1

       230             08/01/13            1

       500             08/01/13            1

       200             08/01/13            1

        53              15/01/13            1

        53              22/01/13            1

Now, I want to get the average for that month (average per day).

SELECT  sum(sf.ORDER_QNT)/31 as AVGPERDAY

FROM    sales_fact sf,

        date_dim dd

WHERE   dd.date_id = sf.date_id

AND     dd.MONTH_NUMBER = 1;

AVGPERDAY  MONTH_NUMBER

----------             ------------

113.785714      1

but instead putting 31, I'd like to pull the totaldays from the actual_date using the Extract function so I try this

SELECT  sum(sf.ORDER_QNT)/EXTRACT(DAY FROM LAST_DAY(to_date('05/01/13','dd/mm/rr'))) as AVGPERDAY,

        dd.month_number

FROM    sales_fact sf,

        date_dim dd

WHERE   dd.date_id = sf.date_id

AND     dd.month_number = 1

GROUP BY dd.month_number;

AVGPERDAY    MONTH_NUMBER

----------               ------------

113.785714        1

The result is nice, but now when I change the date with the dd.actual_date it gives error

SELECT  sum(sf.ORDER_QNT)/EXTRACT(DAY FROM LAST_DAY(dd.actual_date)) as AVGPERDAY,

        dd.month_number

FROM    sales_fact sf,

        date_dim dd

WHERE   dd.date_id = sf.date_id

AND     dd.month_number = 1

GROUP BY dd.month_number;

Error at Command Line : 1 Column : 53

Error report -

SQL Error: ORA-00979: not a GROUP BY expression

00979. 00000 -  "not a GROUP BY expression"


can anyone help?

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 6 2014
Added on Oct 9 2014
3 comments
2,542 views