請教一個重排序問題?

kisscong 2007-10-25 01:35:01
現在有表 A
Num Shop Type
a Z
b Z
c Z
d ZZ
f ZZ
g ZZZ
h ZZZ
現在要求根據不同Type的店鋪為他們排列, 不同Type都從1開始, 結果應該是
Num Shop Type
1 a Z
2 b Z
3 c Z
1 d ZZ
2 f ZZ
1 g ZZZ
2 h ZZZ
...全文
69 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2007-10-25
  • 打赏
  • 举报
回复
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 行)
*/
中国风 2007-10-25
  • 打赏
  • 举报
回复
SELECT
(SELECT COUNT(1) FROM TABLE1 WHERE TYPE=T.TYPE AND SHOP!>T.SHOP) AS NUM
,*
FROM TABLE1 T
fa_ge 2007-10-25
  • 打赏
  • 举报
回复
慢了,
fa_ge 2007-10-25
  • 打赏
  • 举报
回复

select num=(select count(1) from t where type=a.type and shop<=a.shop),
shop,type
from t a
OracleRoob 2007-10-25
  • 打赏
  • 举报
回复

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

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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