The attached stored proc is supposed to create a resultset that contains two columns, one showing the number of orders and the second showing the timetaken.
However, it errors stating that timetaken is an invalid column name!?
What am I doing wrong?
SELECT COUNT(o.orders_id) AS ordercount, (DATEDIFF(DAY, o.date_purchased, osh.orders_status_history_timestamp)) AS timetaken
FROM orders O, dbo.tblPS_orders_status_history OSH WHERE
o.orders_status = 4
AND o.paymentMethodID = 1
AND o.orders_statusID = OSH.orders_status_history_id
AND DATEDIFF(DAY, o.date_purchased, GETDATE()) < 31
GROUP BY timetaken
ORDER BY timetaken