I have this query that gives me back more than one result. I would like to edit it so that it only will give me one result per o.Customer_ID and then have that be the order that would be closest to today with o.OrderPlaceServerTime.
Here’s the Query I have:
SELECT
o.Order_ID,
o.Customer_ID,
o.OrderPlaceServerTime,
o.CustomerOrderTotal
FROM
Orders o
LEFT JOIN Order_LineDetails oln
ON oln.Order_ID = o.Order_ID
WHERE
o.OrderPlaceServerTime >= '2012-09-01 00:00:00'
AND o.OrderPlaceServerTime <= '2012-12-01 00:00:00'
AND o.CustomerOrderTotal >= '50'
AND oln.Product_ID = '75'
What would I need to change to achieve this?
So, first you should get the latest order for the qualifying Customer IDs, by grouping the qualifying order by customer ID and picking the max or OrderPlaceServerTime. Then you would join those records with the Orders on customer ID and the OrderPlaceServerTime to pick the other two attributes of interest.