数据表按类别排序并更新序号的问题

beijietaozi 2011-04-11 10:34:33
SQL数据库中原有如下表t1:
id(int) 标识、自动增长,TypeNum(int),name(char)
数据大概如下:
id,TypeNum,name
1,1,名称1
2,1,名称2
3,1,名称3
4,2,名称4
5,2,名称5
6,3,名称6

现在想在表添加一个新列SortNum(int),这个列是以TypeNum为分类进行排序并更新批量SortNum
效果应该是这样的:
id,TypeNum,name,SortNum
1,1,名称1,1
2,1,名称2,2
3,1,名称3,3
4,2,名称4,1
5,2,名称5,2
6,3,名称6,1

请问各位大侠这SQL语句要如何实现?
...全文
364 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
beijietaozi 2011-04-11
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wufeng4552 的回复:]

引用 11 楼 beijietaozi 的回复:
引用 10 楼 wufeng4552 的回复:

引用 9 楼 beijietaozi 的回复:
引用 7 楼 wufeng4552 的回复:

SQL code
if not object_id('tb') is null
drop table tb
Go
Create table tb([id] int,[TypeNum]……
[/Quote]
执行了,这个也是可以的,谢谢你啊。
不好意思,刚才看到前面的重新插入数据就没仔细往下看
水族杰纶 2011-04-11
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 beijietaozi 的回复:]
引用 10 楼 wufeng4552 的回复:

引用 9 楼 beijietaozi 的回复:
引用 7 楼 wufeng4552 的回复:

SQL code
if not object_id('tb') is null
drop table tb
Go
Create table tb([id] int,[TypeNum] int,[name] nvarchar(3),[So……
[/Quote]
你确定这个不是更新所有?
beijietaozi 2011-04-11
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 wufeng4552 的回复:]

引用 9 楼 beijietaozi 的回复:
引用 7 楼 wufeng4552 的回复:

SQL code
if not object_id('tb') is null
drop table tb
Go
Create table tb([id] int,[TypeNum] int,[name] nvarchar(3),[SortNum] int)
Insert tb
se……
[/Quote]
是要更新啊,但你这个要重新插入数据,那我表里原有的大量数据怎么办
我把前面的查询改一下就可以了
update tb set  SortNum = (select count(*) from tb where TypeNum = t.TypeNum and id <= t.id) from  tb t


水族杰纶 2011-04-11
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 beijietaozi 的回复:]
引用 7 楼 wufeng4552 的回复:

SQL code
if not object_id('tb') is null
drop table tb
Go
Create table tb([id] int,[TypeNum] int,[name] nvarchar(3),[SortNum] int)
Insert tb
select 1,1,N'名称1',null union……
[/Quote]
没搞清楚你到底要查询还是更新
我写的是更新
和查询比
没有可比性
beijietaozi 2011-04-11
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wufeng4552 的回复:]

SQL code
if not object_id('tb') is null
drop table tb
Go
Create table tb([id] int,[TypeNum] int,[name] nvarchar(3),[SortNum] int)
Insert tb
select 1,1,N'名称1',null union all
select 2,1,N'名称2',nul……
[/Quote]
如果原表有大量数据的话这个就不行了
beijietaozi 2011-04-11
  • 打赏
  • 举报
回复
谢谢acupofnescafe,AcHerat,fredrickhu的提示,已经解决了
水族杰纶 2011-04-11
  • 打赏
  • 举报
回复
if not object_id('tb') is null
drop table tb
Go
Create table tb([id] int,[TypeNum] int,[name] nvarchar(3),[SortNum] int)
Insert tb
select 1,1,N'名称1',null union all
select 2,1,N'名称2',null union all
select 3,1,N'名称3',null union all
select 4,2,N'名称4',null union all
select 5,2,N'名称5',null union all
select 6,3,N'名称6',null
Go
declare @id int,@typenum int
set @id = 0
set @typenum =0
update tb set [SortNum] = case when [TypeNum] = @typenum then @id else [SortNum] end,
@id = case when [TypeNum] = @typenum then @id + 1 else 1 end,
@typenum = [TypeNum]
Select * from tb
/*
id TypeNum name SortNum
----------- ----------- ---- -----------
1 1 名称1 1
2 1 名称2 2
3 1 名称3 3
4 2 名称4 1
5 2 名称5 2
6 3 名称6 1

(6 row(s) affected)
*/
水族杰纶 2011-04-11
  • 打赏
  • 举报
回复

都是查询
--小F-- 2011-04-11
  • 打赏
  • 举报
回复
写反了..上面是2005 下面是2000
--小F-- 2011-04-11
  • 打赏
  • 举报
回复
--2000
select
*,SortNum = row_number() over (parition by TypeNum order by getdate())
from
tb

-2005
select
*,SortNum = (select count(*) from tb where TypeNum = t.TypeNum and id <= t.id)
from
tb t
AcHerat 2011-04-11
  • 打赏
  • 举报
回复
select *,SortNum = (select count(*) from tb where TypeNum = t.TypeNum and id <= t.id)
from tb t
AcHerat 2011-04-11
  • 打赏
  • 举报
回复
select *,SortNum = row_number() over (parition by TypeNum order by getdate())
from tb
幸运的意外 2011-04-11
  • 打赏
  • 举报
回复
select *, SortNum = (select count(1) + 1 from t1 B where A.TypeNum = B.TypeNum and A.id > B.id) from t1 A
So_CooL 2011-04-11
  • 打赏
  • 举报
回复
if not object_id('tb') is null
drop table tb
Go
Create table tb([id] int,[TypeNum] int,[name] nvarchar(3),[SortNum] int)
Insert tb
select 1,1,N'名称1',null union all
select 2,1,N'名称2',null union all
select 3,1,N'名称3',null union all
select 4,2,N'名称4',null union all
select 5,2,N'名称5',null union all
select 6,3,N'名称6',null
Go

select id,typeNum,name,SortNum =ROW_NUMBER() over(partition by TypeNum order by id) from tb

id typeNum name SortNum
----------- ----------- ---- --------------------
1 1 名称1 1
2 1 名称2 2
3 1 名称3 3
4 2 名称4 1
5 2 名称5 2
6 3 名称6 1

(6 行受影响)

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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