Order by descending on taking the top 1 row:
SELECT TOP 1 COUNT(SalesOrderID) AS Sales,
TerritoryID
FROM Sales.SalesOrderHeader
GROUP BY TerritoryID
ORDER BY Sales DESC
If you need all rows that have the same max amount of sales, it is more involved:
Here is a way to achieve this with some example data:
DECLARE @SalesOrerHeader TABLE
(
SalesOrderID INT IDENTITY(1,1),
TerritoryID INT
)
INSERT INTO @SalesOrerHeader (TerritoryID) VALUES
(1),(1),(2),(2),(2),(2),(3),(4),(4),(4),(5),(5),(5),(5)
DECLARE @final TABLE
(
TerritoryID INT,
NumberOfSales INT
)
INSERT INTO @final
SELECT TerritoryID, COUNT(SalesOrderID) AS NumberOfSales FROM @SalesOrerHeader
GROUP BY TerritoryID
SELECT * FROM @final
WHERE NumberOfSales = (SELECT MAX(NumberOfSales) FROM @final)
OUTPUT:
TerritoryID NumberOfSales
2 4
5 4