如果我的意見是正確的(600應該是6,7),然後看看這個
DECLARE @Table TABLE(
regno INT,
rowid INT
)
INSERT INTO @Table (regno,rowid) SELECT 100, NULL
INSERT INTO @Table (regno,rowid) SELECT 100, NULL
INSERT INTO @Table (regno,rowid) SELECT 100, NULL
INSERT INTO @Table (regno,rowid) SELECT 200, NULL
INSERT INTO @Table (regno,rowid) SELECT 300, NULL
INSERT INTO @Table (regno,rowid) SELECT 300, NULL
INSERT INTO @Table (regno,rowid) SELECT 400, NULL
INSERT INTO @Table (regno,rowid) SELECT 500, NULL
INSERT INTO @Table (regno,rowid) SELECT 600, NULL
INSERT INTO @Table (regno,rowid) SELECT 600, NULL
DECLARE @TempTable TABLE(
ID INT IDENTITY(1,1),
regno INT
)
INSERT INTO @TempTable (regno)
SELECT regno
FROM @Table
SELECT regno,
CASE
WHEN (SELECT COUNT(1) FROM @TempTable WHERE regno = t.regno) = 1
THEN NULL
ELSE (SELECT COUNT(1) FROM @TempTable WHERE regno = t.regno) - (SELECT COUNT(1) FROM @TempTable WHERE regno = t.regno AND ID > t.ID) +
(SELECT COUNT(1) FROM @TempTable WHERE regno < t.regno AND regno IN (SELECT regno FROM @TempTable GROUP BY regno having COUNT(1) > 1))
END Val
FROM @TempTable t
你的意思是INSERT如你所說(從一個現有的新表中),還是真的是一個更新? – RickNZ 2009-11-26 05:36:21
我認爲OP意味着更新。這從數據和請求中是有意義的。 – 2009-11-26 05:38:17
RANK是2000年唯一可用的排名函數,我知道這個鏈接是我能找到的用於生成rowid的最佳排名,但它不會允許您跳過行:http://support.microsoft.com/kb/186133 – 2009-11-26 06:15:36