我有这样的表
file_nbr name_seq Person_name
10 1 James Linson
10 1 Ronn Dave
10 1 Michael Meyer
12 1 Pamela J. Mayberry
12 1 Randall M. Bachtel
12 1 Cleary E. Mahaffey
12 1 D. Scott Rowley
12 1 Stephen L. Phelps
12 1 Mark A. Bennet
12 1 Richard P. Lewis
我想更改name_seq,结果如下:
10 1 James Linson
10 2 Ronn Dave
10 3 Michael Meyer
12 1 Pamela J. Mayberry
12 2 Randall M. Bachtel
12 3 Cleary E. Mahaffey
12 4 D. Scott Rowley
12 5 Stephen L. Phelps
12 6 Mark A. Bennet
12 7 Richard P. Lewis
什么是最好的SQL查询?
答案 0 :(得分:4)
WITH records
AS
(
SELECT "file_nbr",
ROW_NUMBER() OVER(PARTITION BY "file_nbr" ORDER BY "file_nbr") "name_seq",
"Person_name"
FROM TableName
)
SELECT "file_nbr", "name_seq", "Person_name"
FROM records