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
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
--*/
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
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-2
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,#t