[英]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. 我正在运行此查询到SQL Server 2008+但它不适用于SQL Server 2000.我需要执行此操作。
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. 在SQL Server 2005之前,通过对带有
IDENTITY
列的#temp
表中的有序插入来解决此问题域,以生成序列号。 This would solve RANK()
and ROW_NUMBER()
requirements. 这将解决
RANK()
和ROW_NUMBER()
要求。
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. 此时,您可以查询每个custnum / custname分组的
MIN()
,并在使用CTE时使用它。
However ... is ROW_NUMBER()
really what you want here ? 但是......
ROW_NUMBER()
真的是你想要的吗? Seems like you'd want RANK()
, not ROW_NUMBER()
. 好像你想要
RANK()
,而不是ROW_NUMBER()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.