求写法

solidvacuum 2012-03-12 09:36:23
描述:
tb中有一个字段
类别
a
a
b
b
b
c
c
现在想加个字段形成如下内容
类别 排序
a 0001
a 0002
b 0001
b 0002
b 0003
c 0001
c 0002
请问我该怎么弄,直接在企业管理器设计字段能实现最好
...全文
139 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 chinajiyong 的回复:]

alter table T add 字段名 varchar
不就完了
[/Quote]

怎么能随便修改表的结构或者字段类型??楼上正解,不过这个修改字段类型不可取
唐诗三百首 2012-03-12
  • 打赏
  • 举报
回复

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 0001
a 0002
b 0001
b 0002
b 0003
c 0001
c 0002

(7 row(s) affected)
勿勿 2012-03-12
  • 打赏
  • 举报
回复
--tb中有一个字段
--类别
--a
--a
--b
--b
--b
--c
--c
--现在想加个字段形成如下内容
--类别 排序
--a 0001
--a 0002
--b 0001
--b 0002
--b 0003
--c 0001
--c 0002
--请问我该怎么弄,直接在企业管理器设计字段能实现最好
declare @tb table (类别 varchar(5))
insert into @tb values( 'a'),('a'),('b'),('b'),('b'),('c'),('c')
select *,'000'+ cast (ROW_NUMBER()over(partition by 类别 order by getdate()) as varchar(50))as 排序 from @tb
类别 排序
----- -----------------------------------------------------
a 0001
a 0002
b 0001
b 0002
b 0003
c 0001
c 0002

(7 行受影响)
EnForGrass 2012-03-12
  • 打赏
  • 举报
回复
alter table T add 字段名 varchar
不就完了
SqlServer2008 2012-03-12
  • 打赏
  • 举报
回复

declare @tab table
(
V nvarchar(2)
)

insert into @tab(v)
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 V,right('0000'+convert(nvarchar(4),row_number() over(partition by V order by V)),4) from @tab


a 0001
a 0002
b 0001
b 0002
b 0003
c 0001
c 0002
Felixzhaowenzhong 2012-03-12
  • 打赏
  • 举报
回复

create table tab
(
V nvarchar(2)
)

insert into tab(v)
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'
alter table tab add sort_rownum varchar(32)

update tab set tab.sort_rownum=b.sort_rownum from (select *,row_number() over(order by v )as rn from tab) tab ,
(select V,row_number() over(order by v )as rn,right('0000'+convert(nvarchar(4),row_number() over(partition by V order by V)),4)as sort_rownum from tab)as b
where tab.V =b.V and tab.rn=b.rn
/*
V sort_rownum
a 0001
a 0002
b 0001
b 0002
b 0003
c 0001
c 0002
*/
ILOVE_ASPNET 2012-03-12
  • 打赏
  • 举报
回复
嗯,楼上的说的很对,先分组,然后做个字符串的拼接就可以了。
dawugui 2012-03-12
  • 打赏
  • 举报
回复
--用查询实现

--sql 2005
select t.类别 , 排序 = right('000'+cast(row_number() over(partition by t.类别 order by t.类别) as varchar) ,4) from tb t

--sql 2000用临时表
select t.* , id = identity(int,1,1) into tmp from tb t

select t.类别 , 排序 = right('000'+cast((select count(1) from tmp where 类别 = t.类别 and id < t.id) + 1 as varchar) ,4) from tmp t

22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧