一个有代表性的老问题,高手帮忙呀!

westbulls 2003-12-27 02:34:26
有一个表数据如下:
id num
1 32
2 120
3 44
4 72

要求结果如下:
id num
1 32
2 40
2 40
2 40
3 40
3 4
4 40
4 32
即将num以40为阶段输出,其是id是唯一性字段,好象记得过去有人解决过,但实在是找不到了,请高手再帮忙啦!
...全文
21 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
westbulls 2003-12-27
  • 打赏
  • 举报
回复
多谢各位啦!"结帐啦!"
shuiniu 2003-12-27
  • 打赏
  • 举报
回复
谢谢zjcxc(邹建)的提醒!
发现写法还有一个漏洞!
改一下!

declare @count int
select @count = ceiling(max(num) * 1.0 /40) from @t
set rowcount @count

还有Jianli2004(健力) 的方法也不错!
zjcxc 元老 2003-12-27
  • 打赏
  • 举报
回复
--楼上的改法,还是建议改一下这句

select id=identity(int,0,1) into #t from sysobjects

--改为:
select id=identity(int,0,1) into #t from sysobjects a,sysobjects b

--不然可能处理有些数据不能正确被处理的情况.
Jianli2004 2003-12-27
  • 打赏
  • 举报
回复
不用ID的办法

create table #1(id int,num int)
insert into #1 values(1,32)
insert into #1 values(2,120)
insert into #1 values(3,44)
insert into #1 values(4,72)

select * into #2 from #1 where num>40

while exists (select * from #2 where num>40)
begin
insert into #2 select id,40 as num from #2 where num>40
update #2 set num=num-40 where num>40
end

select * from #2
union all
select * from #1 where num<=40
order by id,num desc
victorycyz 2003-12-27
  • 打赏
  • 举报
回复
嗯,上面已有修正版了。我的可以略过了。
victorycyz 2003-12-27
  • 打赏
  • 举报
回复
邹建的创建临时表的语句需要完善一下:

--select top 3 id=identity(int,0,1) into #t from sysobjects a,sysobjects b
改成这样:

declare @m int
select @m=max(num) / 40 +1 from @t
set rowcount @m
select id=identity(int,0,1) into #t from sysobjects a,sysobjects b
set rowcount 0
shuiniu 2003-12-27
  • 打赏
  • 举报
回复
--在邹建的基础上改进一下!
--测试数据
declare @t table(id int,num int)
insert into @t
select 1,32
union all select 2,232
union all select 3,44
union all select 4,72
--创建一个辅助的临时表
declare @count int
select @count = max(num)/40 + 1 from @t
set rowcount @count
select id=identity(int,0,1) into #t from sysobjects
set rowcount 0
--得到查询结果
select a.id
,num=case when a.num<(b.id+1)*40 then a.num-b.id*40 else 40 end
from @t a join #t b on a.num>=b.id*40
order by a.id,num desc
go
drop table #t
/*
id num
----------- -----------
1 32
2 40
2 40
2 40
2 40
2 40
2 32
3 40
3 4
4 40
4 32

(所影响的行数为 11 行)
*/
zjcxc 元老 2003-12-27
  • 打赏
  • 举报
回复
--下面是测试

--测试数据
declare @t table(id int,num int)
insert into @t
select 1,32
union all select 2,120
union all select 3,44
union all select 4,72

--创建一个辅助的临时表
select top 3 id=identity(int,0,1) into #t from sysobjects a,sysobjects b

--得到查询结果
select a.id
,num=case when a.num<(b.id+1)*40 then a.num-b.id*40 else 40 end
from @t a join #t b on a.num>=b.id*40
order by a.id,num desc
go

--删除临时表
drop table #t

/*--测试结果
id num
----------- -----------
1 32
2 40
2 40
2 40
3 40
3 4
4 40
4 32

(所影响的行数为 8 行)
--*/
zjcxc 元老 2003-12-27
  • 打赏
  • 举报
回复
--还要改一下,顺序不对:

--创建一个辅助的临时表
select top 3 id=identity(int,0,1) into #t from sysobjects a,sysobjects b

--得到查询结果
select a.id
,num=case when a.num<(b.id+1)*40 then a.num-b.id*40 else 40 end
from 表 a join #t b on a.num>=b.id*40
order by a.id,num desc
go

--删除临时表
drop table #t

zjcxc 元老 2003-12-27
  • 打赏
  • 举报
回复
--上面的字段写错了,改一下:

--创建一个辅助的临时表
select top 3 id=identity(int,0,1) into #t from sysobjects a,sysobjects b

--得到查询结果
select a.id
,num=case when a.num<(b.id+1)*40 then a.num-b.id*40 else 40 end
from 表 a join #t b on a.num>=b.id*40
order by a.id

--删除临时表
drop table #t
zjcxc 元老 2003-12-27
  • 打赏
  • 举报
回复
--创建一个辅助的临时表
select top 3 id=identity(int,0,1) into #t from sysobjects a,sysobjects b

--得到查询结果
select a.id
,a.num
,case when a.num<(b.id+1)*40 then a.num-b.id*40 else 40 end
from 表 a join #t b on a.num>=b.id*40
order by a.id

--删除临时表
drop table #t

34,588

社区成员

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

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