简体   繁体   中英

How to convert this query to SQL Server 2000 syntax

I am running this query to SQL Server 2008+ but it doesn't work on SQL Server 2000. and i need this to execute.

WITH CTE AS ( 
    SELECT
        custnum,
        custname,
        RN = ROW_NUMBER() OVER( PARTITION BY custnum, custname ORDER BY custnum )
    FROM
        cust
)
SELECT
    *
FROM
    CTE
WHERE RN > 1

thank you so much for your help!

See if this works

select custnum,custname from
(
select (select count(*) from cust as t1 where t1.custnum<=t2.custnum) as sno,
 custnum,custname from cust as t2
) as t
where sno>1

Prior to SQL Server 2005, this problem domain was solved with ordered inserts into a #temp table with an IDENTITY column to generate the sequence number. This would solve RANK() and ROW_NUMBER() requirements.

Eg:

    -- Create empty temp table
    SELECT custnum, custname, IDENTITY(INT, 1, 1) as RN
    INTO #cust
    FROM cust
    WHERE 0 = 1

    -- Populate with ORDER BY, to generate ascending RN
    INSERT INTO #cust
        (custnum, custname)
    SELECT
        custnum, custname
    FROM 
        cust
    ORDER BY
        custnum, custname

At that point, you can query MIN() for each custnum/custname grouping and use that as you'd use the CTE.

However ... is ROW_NUMBER() really what you want here ? Seems like you'd want RANK() , not ROW_NUMBER() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM