123再到123怎么搞

SKY1006 2005-11-24 03:08:52

表:Table1

部门 姓名
技术部 小李
技术部 小王
技术部 小张
市场部 小明
市场部 小军
市场部 小红

要生成的表

序号 部门 姓名
1 技术部 小李
2 技术部 小王
3 技术部 小张
1 市场部 小明
2 市场部 小军
3 市场部 小红







...全文
196 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
diaowf 2005-11-30
  • 打赏
  • 举报
回复
select sum(1) 是用来计数的!
就像select sum(*) or count(*)一样!
diaowf 2005-11-29
  • 打赏
  • 举报
回复
Top
(select sum(1) from table1 where 部门=A.部门 and 姓名<=A.姓名)
select sum(1)是怎么用呀???
谢谢!
ch02645 2005-11-27
  • 打赏
  • 举报
回复
建好 [Table2]
insert into Table2 select * from table1
kofboyx 2005-11-26
  • 打赏
  • 举报
回复
用三个表
部门表(部门ID, 部门名称)
人员表(人员ID, 人员名称)
部门人员(部门ID, 人员ID)
zlp321002 2005-11-25
  • 打赏
  • 举报
回复
select
序号=(select sum(1) from table1 where 部门=A.部门 and 姓名<=A.姓名),
部门,
姓名
from
table1 A
order by
部门,
(select sum(1) from table1 where 部门=A.部门 and 姓名<=A.姓名)
拓狼 2005-11-25
  • 打赏
  • 举报
回复
alter table table1 add id int

update table1 set id=(select sum(1) from table1 where 部门=a.部门 and 姓名<=a.姓名 )
from table1 a
nononono 2005-11-25
  • 打赏
  • 举报
回复
soulwin(铁血汉子)改的对!
soulwin 2005-11-24
  • 打赏
  • 举报
回复
#t1 别名 a

select IDENTITY(int, 1,1) AS id, 部门, 姓名 into #t1 from table1 order by 部门, 姓名
select a.id-b.id+1 as 序号, a.部门, a.姓名 into table2 from #t1 a inner join (select min(id) id ,部门 from #t1 group by 部门) b on a.部门=b.部门
SKY1006 2005-11-24
  • 打赏
  • 举报
回复
--to 楼上的,不行呀
--建立测试环境
create Table table1(
部门 varchar(50),
姓名 varchar(50)
)

insert into table1 select '技术部','小李'
insert into table1 select '技术部','小王'
insert into table1 select '技术部','小张'
insert into table1 select '市场部','小明'
insert into table1 select '市场部','小军'
insert into table1 select '市场部','小红'


--要成的是table2
select IDENTITY(int, 1,1) AS id, 部门, 姓名 into #t1 from table1 order by 部门, 姓名
select a.id-b.id+1 as 序号, a.部门, a.姓名 into table2 from #t1 inner join (select min(id) id ,部门 from #t1 group by 部门) b on a.部门=b.部门

select * from table2

drop table table1
drop table table2
drop table #t1

---提示
/*
服务器: 消息 107,级别 16,状态 2,行 5
列前缀 'a' 与查询中所用的表名或别名不匹配。
服务器: 消息 107,级别 16,状态 1,行 5
列前缀 'a' 与查询中所用的表名或别名不匹配。
服务器: 消息 107,级别 16,状态 1,行 5
列前缀 'a' 与查询中所用的表名或别名不匹配。
服务器: 消息 107,级别 16,状态 1,行 5
列前缀 'a' 与查询中所用的表名或别名不匹配。
*/

nononono 2005-11-24
  • 打赏
  • 举报
回复
还有错!

select IDENTITY(int, 1,1) AS id, 部门, 姓名 into #t1 from table1 order by 部门, 姓名
select a.id-b.id+1 as 序号, a.部门, a.姓名 into 要生成的表 from #t1 inner join (select min(id) id ,部门 from #t1 group by 部门) b on a.部门=b.部门

nononono 2005-11-24
  • 打赏
  • 举报
回复
select IDENTITY(int, 1,1) AS id, 部门, 姓名 into #t1 from table1 order by 部门, 姓名
select a.id-b.id+1 as 序号, a.部门, a.姓名 into 要生成的表 from #t1 a inner join (select min(id),部门 from #t1 group by 部门) b on a.部门=b.部门
nononono 2005-11-24
  • 打赏
  • 举报
回复
select IDENTITY(int, 1,1) AS id, 部门, 姓名 into #t1 from table1 order by 部门, 姓名
select a.id-b.id+1 as 序号, a.部门, a.姓名 into 要生成的表 from #t1 inner join (select min(id),部门 from #t1 group by 部门) b on a.部门=b.部门


27,579

社区成员

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

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