读书人

分组显示行号,该怎么处理

发布时间: 2012-03-23 12:06:21 作者: rapoo

分组显示行号
如何分组显示行号
就是比如字段组号,对每组下的记录自动编号,显示出来,查询时增加一个编号字段,结果如下:
编号 组号
a1 a
a2 a
a3 a
b1 b
b2 b
c1 c
d1 d
d2 d

[解决办法]
老办法,相关子查询~
效率不高,想提高效率,用SQL 2005
declare @t table(item varchar(10),groupname varchar)
insert @t select 'a1 ', 'a '
union all select 'a2 ', 'a '
union all select 'a3 ', 'a '
union all select 'b1 ', 'b '
union all select 'b2 ', 'b '
union all select 'c1 ', 'c '
union all select 'd1 ', 'd '
union all select 'd2 ', 'd '

select * from @t

select *,
ranks=1+(select count(1) from @t B where A.groupname=groupname and item <A.item)
from @t A

读书人网 >.NET

热点推荐