求一SQL语句,为一个表添加一个临时编号?
create table test(clothid varchar(10),number varchar(10),bagid varchar(10))
insert into test
select '11-11','56','1'
union all select '11-12-1','45','1'
union all select '11-12-2','46','1'
union all select '11-12-3','50','2'
union all select '11-13-1','60','2'
union all select '11-13-2','32','2'
union all select '11-14','34','3'
union all select '11-15-1','45','3'
union all select '11-15-2','52','3'
结果如下:
id number bagid clothid
1 56 1 11-11
2 45 1 11-12-1
3 46 1 11-12-2
1 50 2 11-12-3
2 60 2 11-13-1
3 32 2 11-13-2
1 34 3 11-14
2 45 3 11-15-1
3 52 3 11-15-2
问题点数:20、回复次数:4Top
1 楼zjcxc(邹建)回复于 2005-06-04 11:23:16 得分 5
create table test(clothid varchar(10),number varchar(10),bagid varchar(10))
insert into test
select '11-11','56','1'
union all select '11-12-1','45','1'
union all select '11-12-2','46','1'
union all select '11-12-3','50','2'
union all select '11-13-1','60','2'
union all select '11-13-2','32','2'
union all select '11-14','34','3'
union all select '11-15-1','45','3'
union all select '11-15-2','52','3'
go
select id=(select count(*) from test where clothid<=a.clothid),
number,bagid,clothid
from test a
order by id
go
drop table test
/*=--结果
id number bagid clothid
----------- ---------- ---------- ----------
1 56 1 11-11
2 45 1 11-12-1
3 46 1 11-12-2
4 50 2 11-12-3
5 60 2 11-13-1
6 32 2 11-13-2
7 34 3 11-14
8 45 3 11-15-1
9 52 3 11-15-2
(所影响的行数为 9 行)
--*/Top
2 楼xluzhong(Ralph)回复于 2005-06-04 11:25:06 得分 5
select sid=identity(int,1,1),* into #t from test
select
id=(select count(*) from #t where bagid=a.bagid and sid<=a.sid),
number,
bagid,
clothid
from #t a
Top
3 楼hsj20041004(光芒)回复于 2005-06-04 11:31:09 得分 5
To: zjcxc(邹建)
你是不是看错题了!!!
结果如下:
id number bagid clothid
1 56 1 11-11
2 45 1 11-12-1
3 46 1 11-12-2
1 50 2 11-12-3
2 60 2 11-13-1
3 32 2 11-13-2
1 34 3 11-14
2 45 3 11-15-1
3 52 3 11-15-2Top
4 楼xluzhong(Ralph)回复于 2005-06-04 11:55:23 得分 5
create table test(clothid varchar(10),number varchar(10),bagid varchar(10))
insert into test
select '11-11','56','1'
union all select '11-12-1','45','1'
union all select '11-12-2','46','1'
union all select '11-12-3','50','2'
union all select '11-13-1','60','2'
union all select '11-13-2','32','2'
union all select '11-14','34','3'
union all select '11-15-1','45','3'
union all select '11-15-2','52','3'
go
select sid=identity(int,1,1),* into #t from test
select
id=(select count(*) from #t where bagid=a.bagid and sid<=a.sid),
number,
bagid,
clothid
from #t a
drop table test,#tTop




