Dynamic date selections
I use the following query to get the average days per order, for a month. It sums the datediff of all the dates and divides by the total number of open orders for the month,in this case Jun 09, which I hardcoded.
SELECT Avg_Aging, Site FROM
(
SELECT DISTINCT CAST(CAST(SUM(DateDiff(day,Date_Arrived, GETDATE()) + 1 ) AS DECIMAL)
/CAST(COUNT(orderNumber)AS DECIMAL) AS DECIMAL (10,1)) AS Avg_Aging, Site
FROM ordersTable INNER JOIN siteMasterr
ON ordersTable.Site_ID = siteMasterr.Site_ID
WHERE status NOT IN ('Voided','Closed')
and date_arrived >= '06/01/2009' and date_arrived < '07/01/2009'
GROUP BY Site
) AS I
where site = 'Florida'
What I need to know is, instead of hardcoding the date_arrived, for each month, how can I do it dynamically, so that each time I run this, it will get all the data for Jan 09, then Feb 09, etc., up to Jun 09. July 09 is not complete yet, so it should not do anything else after Jun.
