wow it's been a long time since i had to do this in SQL2000;
SQL 2000 doesn't support row_number() functions either.
if you can go without row_number, it just becomes a sub select:
select
territoryID
, salesPersonID
, SalesTotal
from (select
TerritoryID,
salesPersonID,
sum(TotalDue) as salesTotal
from [Sales].[SalesOrderHeader]
where SalesPersonID is not null
group by TerritoryID, SalesPersonID
) ST
--where RowID <=3
order by territoryID
if you need row_number, it's going to require a tempt table, and updating the temp table with a neat trick i learned form Jeff Moden; unfortunately, it cannot be done in a single statement, either.
this is untested, since i don't have the sample data, but this should be kind of close:
--===== Create a test table.
-- This is NOT part of the solution.
IF OBJECT_ID('TempDB..#ST') IS NOT NULL
DROP TABLE #ST
--create the temp table, with placeholders for the partition by() of the row_number
select
identity(int,1,1) AS ID,
0 As rowID,
territoryID,
salesPersonID
, SalesTotal
INTO #ST
from (select
TerritoryID,
salesPersonID,
sum(TotalDue) as salesTotal
from [Sales].[SalesOrderHeader]
where SalesPersonID is not null
group by TerritoryID, SalesPersonID ) ST
ORDER BY TerritoryID, SalesPersonID
--we need a clustered index
ALTER TABLE #ST
ADD PRIMARY KEY CLUSTERED (TerritoryID)
DECLARE @PrevTerritoryID INT,
@PrevSeq INT
--update the table and the variable/counter all in the same statemetn.
UPDATE #ST
SET @PrevSeq = rowid = CASE WHEN TerritoryID = @PrevTerritoryID THEN @PrevSeq + 1 ELSE 1 END,
@PrevTerritoryID = TerritoryID
FROM #ST WITH(INDEX(0),TABLOCKX)
--check the results?
SELECT * FROM #ST
--WHERE rowID <=3
ORDER BY TerritoryID
Lowell