SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Making Query SQL 2000 compatable


Making Query SQL 2000 compatable

Author
Message
winston Smith
winston Smith
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1763 Visits: 2064
I have a query i wrote for 2008 to get the top 3 salesPeople per territory by their total sales. Its not too difficult in 2005/2008 with CTE's and Partitioning however i cannot get this to work in SQL 2000 where there are no CTE's.

Id appreciate any help or nudges in the right direction:

Query is a mockup of the actual query, columns, tables and data is all fictional:


with ST AS
( select
ROW_NUMBER() over(partition by TerritoryID order by sum(totalDue) desc) as rowID,
TerritoryID,
salesPersonID,
sum(TotalDue) as salesTotal
from [Sales].[SalesOrderHeader]
where SalesPersonID is not null
group by TerritoryID, SalesPersonID
)
select
territoryID
, salesPersonID
, SalesTotal
from ST
where RowID <=3
order by territoryID




Thanks!
Lowell
Lowell
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27980 Visits: 39922
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Sergiy
Sergiy
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10234 Visits: 11958
There is a simpler way.
No updates required:

--===== Create a test table.  
-- This is NOT part of the solution.
IF OBJECT_ID('TempDB..#ST') IS NOT NULL DROP TABLE #ST

DECLARE @N int
SET @N = 3 -- how many rows to retrieve for each group

--create the temp table, with Identity column playing the role of RowNumber
select
identity(int,1,1) AS ID,
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,
salesTotal DESC -- arranging ID's within the each group

SELECT T.*
FROM #ST T
INNER JOIN (
SELECT TerritoryID,
MIN(ID) FromID, MIN(ID) + @N ToID
-- MIN(ID) is a starting point for rowcount within each group
FROM #ST
GROUP BY TerritoryID
) DT ON DT.TerritoryID = T.TerritoryID
AND T.ID BETWEEN DT.FromID AND DT.ToID
ORDER BY T.TerritoryID, T.ID


ScottPletcher
ScottPletcher
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7782 Visits: 7140
I think this will do it; performance will depend on the indexes available on the table:




select sales.*
from (
select distinct TerritoryID
from [Sales].[SalesOrderHeader]
) AS terrs
inner join
( select top 3
TerritoryID,
salesPersonID,
sum(TotalDue) as salesTotal
from [Sales].[SalesOrderHeader]
where
TerritoryID = terrs.TerritoryID and
SalesPersonID is not null
group by TerritoryID, SalesPersonID
order by sum(TotalDue) DESC
) AS sales on
sales.TerrorityID = terrs.TerritoryID




SQL DBA,SQL Server MVP(07, 08, 09)[size=2]Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial: If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.[/size]
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search