update这个字段的值, 语句怎么写?

okitgo 2006-01-06 08:52:53
我有一个表table1是下面这样:
group_name seq_id
aaa NULL
aaa NULL
aaa NULL
bbb NULL
bbb NULL
ccc NULL
..........
现在我要根据group_name分组,再加上一个序列号,就像下面这样:
group_name seq_id
aaa 1
aaa 2
aaa 3
bbb 1
bbb 2
ccc 1
............
...全文
134 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsqkeke 2006-01-07
  • 打赏
  • 举报
回复
一楼和三楼的方法都一样!
Ninjai_Chan 2006-01-07
  • 打赏
  • 举报
回复
SQL2005有这样的函数吗?要用一下了.呵呵!
楼上的都是正解啊!
lxzm1001 2006-01-06
  • 打赏
  • 举报
回复
create table table1
(group_name varchar(10),
seq_id int)
insert table1 select 'aaa',NULL
union all select 'aaa',NULL
union all select 'aaa',NULL
union all select 'bbb',NULL
union all select 'bbb',NULL
union all select 'ccc',NULL
select id=identity(int),* into #t from table1
update #t set seq_id=(select count(*) from #t where group_name=t.group_name and id<=t.id) from #t t
select group_name,seq_id from #t
zjcxc 2006-01-06
  • 打赏
  • 举报
回复
-- sql 2005
select group_name, seq_id=row_number() over(order by group_name partion by group_name)
from table1
jixiaojie 2006-01-06
  • 打赏
  • 举报
回复
下面的这个需要一个自增列,没有自增列的不知道怎么写


create table tb3
(id int identity(1,1),
group_name varchar(100),
seq_id int
)

insert into tb3
select 'aaa',null union all
select 'aaa',null union all
select 'bbb',null union all
select 'bbb',null union all
select 'aaa',null union all
select 'ccc',null

update tb3
set seq_id =(select count(*) from tb3 a where tb3.id >=a.id and tb3.group_name=a.group_name)


select group_name,seq_id from tb3
order by group_name,seq_id


drop table tb3

/*

aaa 1
aaa 2
aaa 3
bbb 1
bbb 2
ccc 1
*/

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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