SQL SERVER 2000/2005/2008 一句话完成分组后加序号--【叶子】

来源:互联网 发布:美工强大的手游 编辑:程序博客网 时间:2024/04/28 11:16
--测试数据declare @T table (id int identity,col varchar(5))insert into @Tselect 'AAA' union allselect 'AAA' union allselect 'BBB' union allselect 'CCC' union allselect 'CCC'--SQL SERVER 2005/2008select row_number() over (partition by col order by id) as num,col from @T/*num                  col-------------------- -----1                    AAA2                    AAA1                    BBB1                    CCC2                    CCC*/--SQL SERVER 2000select (select count(1) from @T where col=t.col and id<=t.id) as num ,col from @T t/*num         col----------- -----1           AAA2           AAA1           BBB1           CCC2           CCC*/