sql - Getting average per day using the extract and last_day functions -


Here's a quick help, I have a small project here, I have this report with the date of total order Order (this will actually continue with my second question) So what am I doing here

  SELECT sf.ORDER_QNT, dd.ACTUAL_DATE, dd.MONTH_NUMBER to 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 of that month (average per day) Using the Last_day function.

  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_reference sf, date_dim dd WHERE dd.date_id = sf.date_id and dd.month_number = 1 GROUP dd.month_number; AVGPERDAY MONTH_NUMBER -------------------------- 113.785714 1   

The result is good, but now when I date Replace with DD.activ_det, this error returns

  SELECT sum (sf.ORDER_QNT) / extract (day to LAST_DAY (DD. Active_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 dd.month_number; Error in command line: 1 column: 53 error report - SQL error: ORA-009798: Group does not contain expression 00 9 79. 00000 - "no group with expression"   

Can anyone help?

  sum (sf.ORDER_QNT) / decode (round (max (dd.actual_date) Minimum (dd.actual_date)), 0, 1)   

You can use this expression in your query (but the result can not be the same with LAST_DAY)

Or you can extend the part given by a group (DAY FROM LAST_DAY (DD.ActivateDet)):

  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.m Onth_number = 1 GROUP dd.month_number, extract (DAY FROM LAST_DAY (dd.actual_date));    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -