求写法
描述:
tb中有一个字段
类别
a
a
b
b
b
c
c
现在想加个字段形成如下内容
类别 排序
a 0001
a 0002
b 0001
b 0002
b 0003
c 0001
c 0002
请问我该怎么弄,直接在企业管理器设计字段能实现最好
[解决办法]
declare @tab table( V nvarchar(2))insert into @tab(v)select 'a' union allselect 'a' union allselect 'b' union allselect 'b' union allselect 'b' union allselect 'c' union allselect 'c' select V,right('0000'+convert(nvarchar(4),row_number() over(partition by V order by V)),4) from @taba 0001a 0002b 0001b 0002b 0003c 0001c 0002
[解决办法]
create table tb(类别 char(2))insert into tb(类别)select 'a' union all select 'a' union all select 'b' union all select 'b' union all select 'b' union all select 'c' union all select 'c'select 类别,'000'+cast(row_number() over(partition by 类别 order by getdate()) as varchar) '排序'from tb类别 排序---- ---------------------------------a 0001a 0002b 0001b 0002b 0003c 0001c 0002(7 row(s) affected)