下面的查詢可以返回您預期的結果。
SELECT A, B, B + '.' + CAST(ROW_NUMBER() OVER (PARTITION BY B ORDER BY A) AS VARCHAR) AS C
FROM TableName
生成Alpha數值序列很困難。在計算列中使用ROW_NUMBER()
也是不可能的。因此,對於你的情況view
是達到您的期望是正確的選擇:
CREATE VIEW dbo.vw_MyAlphaNumbericOrder AS
SELECT A, B, B + '.' + CAST(ROW_NUMBER() OVER (PARTITION BY B ORDER BY A) AS VARCHAR) AS C
FROM dbo.TableName
所以,當你永遠都插入一個新的記錄,然後SELECT * FROM dbo.vw_MyAlphaNumbericOrder
將與字母數字系列爲C
列返回。
樣品執行與給定的樣本數據:
DECLARE @TestTable TABLE (A INT, B VARCHAR (2));
INSERT INTO @TestTable (A, B) VALUES
(1, 'A'),
(1, 'A'),
(1, 'A'),
(1, 'A'),
(2, 'B'),
(2, 'B'),
(3, 'C'),
(3, 'C'),
(3, 'C');
SELECT A, B, B + '.' + CAST(ROW_NUMBER() OVER (PARTITION BY B ORDER BY A) AS VARCHAR) AS C
FROM @TestTable
使用「身份」列。不要構造人造鑰匙。 –
感謝您的建議。但列A是標識列。你給我查詢的簡單例子 – Perumal
身份重複值?真? – Serg