• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3320
  • Last Modified:

ROW_NUMBER and PARTITION MS SQL Server 2000 translation

I have a query (below) that uses the row_number and partition functions which are supported by MS SQL Server 2005, but not 2000.  Unfortunately, one of the servers I query against is a 2000 server.  How can I write the query below to work in 2000 with the same results?

select SalesNum,
CASE ROW_NUMBER() OVER (PARTITION BY a.LoanNum ORDER BY a.LoanNum)
WHEN 1 THEN 'Y'
ELSE 'N'
END AS FirstRecord
FROM
Table2

SaleNum     FirstRecord
1                          Y
1                          N
2                          Y
3                          Y
3                          N
3                          N
4                          Y
4                          N
4                          N
4                          N
5                          Y
5                          N


CASE ROW_NUMBER() OVER (PARTITION BY a.LoanNum ORDER BY a.LoanNum)
WHEN 1 THEN 'Y'
ELSE 'N'
END AS FirstOccurrence,
0
srejja
Asked:
srejja
1 Solution
 
Anthony PerkinsCommented:
You will have to first write to a temporary table (or variable of type table) that contains an IDENTITY column.  You can then output the results from that temporary table.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
assuming you have some primary key on the table, and it is actually LoadNum, then you can emulate the row_number() function


select salesNum
, case when Row_number = 0 then 'Y' else 'N' end as firstrecord
from (
select SalesNum,
( select count(*) from Table2 i where i.SaleNum = t.SaleNum and i.LoanNum  < t.LoanNum ) as row_number
FROM Table2 t
) as l


hope this helps
0
 
Scott PletcherSenior DBACommented:
You don't require correlated subquery(ies) [or a temp table] just to flag the first row, something like this should do it:


select Table2.SalesNum, CASE WHEN Table2.LoanNum = Table2Min.LoanNum THEN 'Y' ELSE 'N' END AS FirstRecord
FROM
Table2
INNER JOIN (
    SELECT SalesNum, MIN(LoanNum) AS LoanNum
    FROM Table2
    GROUP BY SalesNum
) AS Table2Min ON Table2.salesNum = Table2Min.salesNum
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now