我正在运行此查询到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
非常感谢你的帮助!
答案 0 :(得分:0)
看看是否有效
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
答案 1 :(得分:0)
在SQL Server 2005之前,此问题域通过在#temp
表中按顺序插入并使用IDENTITY
列来生成序列号来解决。这将解决RANK()
和ROW_NUMBER()
要求。
E.g:
-- 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
此时,您可以查询MIN()
每个custnum / custname分组,并在使用CTE时使用它。
然而...... ROW_NUMBER()
真的是你想要的吗?好像你想要RANK()
,而不是ROW_NUMBER()
。