Query that returns Average number of records in a specific range of dates

杀马特。学长 韩版系。学妹 提交于 2019-12-25 13:08:50

问题


I have a table with the following columns

OrderID     Number
ProductID   Number 
Order_date  Datetime
Qty         number

How can I get average number of orders placed per day for a range of dates. I want to know the average orders placed per day between October 31st to November 4th both days included.

Thanks in advance.


回答1:


Your question is a little contradictory, it fisrt asks for the average number of orders per day and then asks for the average orders (wich suggests you want the average quantity, not the average number of orders)

This should give you the averagy QTY for each day for the range you specify (insert your tablename though):

SELECT TRUNC(order_date) AS order_day,
       AVG(qty) AS daily_orders
  FROM <table>
 WHERE order_date 
          BETWEEN TO_DATE('31-OCT-2011 00:00:00', 'DD-MON-YYYY HH24:MI:SS')
              AND TO_DATE('04-NOV-2011 23:59:59', 'DD-MON-YYYY HH24:MI:SS')
 GROUP BY TRUNC(order_date);

If you are actually asking for the average number of orders (not the average quantity) per day over the time period you referenced then you would need to run:

SELECT AVG(orders_per_day)
  FROM (
       SELECT TRUNC(order_date), count(*) AS orders_per_day
         FROM <table>
        WHERE order_date 
               BETWEEN TO_DATE('31-OCT-2011 00:00:00', 'DD-MON-YYYY HH24:MI:SS')
                   AND TO_DATE('04-NOV-2011 23:59:59', 'DD-MON-YYYY HH24:MI:SS')
        GROUP BY TRUNC(order_date)
       );

Hope it helps...



来源:https://stackoverflow.com/questions/8093873/query-that-returns-average-number-of-records-in-a-specific-range-of-dates

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!