读书人

求一SQL 指令写法解决思路

发布时间: 2012-03-04 11:13:34 作者: rapoo

求一SQL 指令写法
如有一表
--
A
B
B
B
C
C
D
想得到如可结果(过滤重复行加一列序号)
----
A 1
B 2
C 3
D 4


[解决办法]
--例子--
create table tab(c varchar(10))
insert tab
select 'A '
union all select 'B '
union all select 'B '
union all select 'B '
union all select 'C '
union all select 'C '
union all select 'D '


select id=(select count(1) from (select c from tab group by c) a where c <=t.c),c from (select c from tab group by c) t


drop table tab

读书人网 >SQL Server

热点推荐