Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Aggregate (MAX) Value in Returned Result Set

$
0
0

I am using SQL Server 2012 and have a simple query.  I need to get the maximum qty for each sales rep and display that quantity along with the order number and the sales rep number in my result set.

Tried code block but it returns all quantities and not just the maximum quantity.  Any help would be appreciated.

Thanks!

SELECT S1.REPID,S1.ORDNUM
FROM SALES S1
WHERE EXISTS
 (SELECT MAX(QTY), ORDNUM
	FROM SALES
	WHERE SALES.ORDNUM = S1.ORDNUM
	GROUP BY ORDNUM
	HAVING MAX(QTY) = S1.QTY)


Viewing all articles
Browse latest Browse all 23857

Trending Articles