2013-06-03 39 views
0

我有一个5000行已经填充在表中的表。如何在已经有记录的表上创建行计数?

我有一个名为SEQN的列。

我想用行计数填充此列。

我使用:

微软SQL Server Management Studio中9.00.4035.00 微软的Analysis Services客户端工具2005.090.4035.00 Microsoft数据访问组件(MDAC)6.1.7601.17514 微软MSXML 3.0 4.0 5.0 6.0 微软互联网Explorer 9.0.8112.16421 Microsoft .NET Framework 2.0.50727.5466 操作系统6.1.7601

感谢您的高级帮助。

SQLNewbie

+0

你有任何偏好的订单或将到5000就够了1-5任分配? – HABO

+0

任何分配将罚款...感谢 – scottgobucks

+0

第二个想法...顺序按时间戳将工作得很好...谢谢 – scottgobucks

回答

0
declare @Foo as Table (FooId Int Identity, Sequence Int, Timestamp DateTime); 
insert into @Foo (Sequence, Timestamp) values 
    (42, '20010203 10:18:05'), (18, '20100508 22:18:05'), (NULL, '19960316 19:00:00'); 

select * from @Foo order by Sequence; 

update Foo 
    set Sequence = S 
    from (select Sequence, Row_Number() over (order by Timestamp) as S from @Foo) as Foo; 

select * from @Foo order by Sequence; 

update Foo 
    set Sequence = S 
    from (select Sequence, Row_Number() over (order by Timestamp desc) as S from @Foo) as Foo; 

select * from @Foo order by Sequence; 
相关问题