如何对分组结果添加知道编号

存在表table1 结构如下
CW Depth1 Depth2 ...
A 2500 2512
B 2511 2513
A 2711 2080
A 2900 2901
B 1153 1787
希望对CW进行分组,按照Depth1进行排序
然后再添加组内的自动编号
如下
CW Depth1 Depth2 ID
A 2500 2512 1
A 2711 2080 2
A 2900 2901 3
B 1153 1787 1
B 2511 2513 2

[325 byte] By [CodeSaint-下划线] at [2008-1-9]
# 1
不好意思.把自动编号写成知道编号了
CodeSaint-下划线 at 2007-10-19 > top of Msdn China Tech,MS-SQL Server,疑难问题...
# 2
这种比较多在客户端实现,很少看到在表里实现的

数据库里,我能够实现的,可能就是游标+变量的方式

不知道其他高手有比较好的方法不?
# 3
可以建立另外一个表,增加一下标识列,将排序后的数据插入,然后再select.
# 4
我觉得楼上的办法是指整体,没有分段排序。
=======================================

说说咱的不成熟的看法(别笑咱啊,呵~~~);看题目是:CW进行分组,按照Depth1进行排序,所以只用这两个字段

select cw, depth1,null ids into #b from table1
order by cw,depth1 asc

update #b set ids=1 from (select cw,min(depth1) depth1 from #b group by cw) m
where #b.cw=m.cw and #b.depth1=m.depth1

SELECT cw,count(*) cs into #cs from gd group by cw-- 循环次数
declare @a int ,@b int

set @a=2
select @b=max(cs) from #cs
while @a<=@b
begin
update #b set ids=@a from (select cw,min(depth1) depth1 from #b where ids is null group by cw) m
where #b.cw=m.cw and #b.depth1=m.depth1
set @a=@a+1
end
结果:select * from #b
xi_ha at 2007-10-19 > top of Msdn China Tech,MS-SQL Server,疑难问题...
# 5

呵呵,不好意思啊,下面的gd表就是table1表,忘记改了
SELECT cw,count(*) cs into #cs from gd group by cw-- 循环次数
xi_ha at 2007-10-19 > top of Msdn China Tech,MS-SQL Server,疑难问题...
# 6
期盼者更多的高手发言
CodeSaint-下划线 at 2007-10-19 > top of Msdn China Tech,MS-SQL Server,疑难问题...
# 7

declare @temptable table (
CW varchar(2) ,
Depth1 int,
Depth2 int
)
insert @temptable select 'A',2500,2512
union select 'B',2511,2513
union select 'A',2500,2512
union select 'A',2711,2080
union select 'A',2900,2901
union select 'B',1153,1787

select * from @temptable

select cw,Depth1,Depth2,ID = (select count(*) from @temptable b where a.depth1 >= b.depth1 and a.cw = b.cw group by cw) from @temptable a order by cw,Depth1
blackmeit-煤炭 at 2007-10-19 > top of Msdn China Tech,MS-SQL Server,疑难问题...