34,588
社区成员
发帖
与我相关
我的任务
分享
create table tb(Shop varchar(10),Type varchar(10))
insert into tb values('a', 'Z' )
insert into tb values('b', 'Z' )
insert into tb values('c', 'Z' )
insert into tb values('d', 'ZZ' )
insert into tb values('f', 'ZZ' )
insert into tb values('g', 'ZZZ' )
insert into tb values('h', 'ZZZ' )
go
select num=(select count(1) from tb where type=a.type and shop<a.shop)+1 , * from tb a order by type , num
drop table tb
/*
num Shop Type
----------- ---------- ----------
1 a Z
2 b Z
3 c Z
1 d ZZ
2 f ZZ
1 g ZZZ
2 h ZZZ
(所影响的行数为 7 行)
*/
select
(select count(*) from 表A where Type=t.Type and Shop<=t.Shop) as Num,
Shop,
Type
from 表A as t
order by type, Shop