SQL select up to the last 6 dates worth of an order
-
Hi, assume I have two tables called Orders (ID, DateCreated) and a child table called OrderDetails (ID, OrderID, ProductID, Quantity) What I need to do is calculate the average last 6 orders. Now if there are only 5 dates worth of orders I would need to know this value as I need to divide the sum(Quantity) by the number of orders - which cannot be greater than 6 previous orders. I have tried a few different select statements, but cannot seem to get the result I am looking for. Oh one other thing, I would be selecting these records by ProductID. Any suggestions at all? Thanks
-
Hi, assume I have two tables called Orders (ID, DateCreated) and a child table called OrderDetails (ID, OrderID, ProductID, Quantity) What I need to do is calculate the average last 6 orders. Now if there are only 5 dates worth of orders I would need to know this value as I need to divide the sum(Quantity) by the number of orders - which cannot be greater than 6 previous orders. I have tried a few different select statements, but cannot seem to get the result I am looking for. Oh one other thing, I would be selecting these records by ProductID. Any suggestions at all? Thanks
I am not quite sure what you want your result set to look like. It would be easier if you gave a list of the resultant columns. Are you looking for the last 6 orders of a particular product or simply the last 6 orders?