I wrote this query to show me the last purchase of each customers using “EXISTS” but it is not working. it returns all of the rows without filtering them. Can you please advise me how to resolve this issue?
Thank you in advance
snipped image
SELECT O1.custid, O1.orderid, O1.orderdate, O1.empid
FROM Sales.Orders AS O1
WHERE EXISTS (
SELECT O2.custid, MAX(O2.orderdate)
FROM Sales.Orders AS O2
WHERE O2.custid=O1.custid
GROUP BY O2.custid
);
New contributor
ArashVN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.