问题
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