使用自动增量序列号Sql server进行分组

时间:2014-05-21 05:30:21

标签: sql-server

我附上了我的问题的图片,请帮助我...... enter image description here

1 个答案:

答案 0 :(得分:1)

<强> Use Ranking Functions

select *,row_number() over(partition by Test_ID order by Test_id) as [#no]
from table_name

如果表格中已存在 [#no] 列,并且您希望更新,请使用以下查询

Group_Id 视为主键

update t1
set t1.[#no]=t2.[#no]
from table_name t1
inner join 
(select Group_Id,row_number() over(partition by Test_ID order by Test_id) as [#no]
from table_name)t2 on t1.Group_Id=t2.Group_Id