关于数值拆分的问题

fflcat 2013-05-09 05:13:33
一个简单数据表结构:
id count
1 1
2 2
3 20
4 21

需要将count>10的做拆分 既
id count
1 1
2 2
3 10
3 10
4 10
4 10
4 1 这样的结果,求解!谢谢
...全文
96 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
撸大湿 2013-05-09
  • 打赏
  • 举报
回复
引用 2 楼 fflcat 的回复:
我用了个循环嵌套加IF判断,好像比较笨的法子。谢谢
不客气,复杂的SQL要好好利用排序笛卡尔积
fflcat 2013-05-09
  • 打赏
  • 举报
回复
[quote=引用 1 楼 tntzbzc 的回复:]
create table # (id int,CT INT)
insert #
select 1,1
union all select 2,2
union all select 3,24
union all select 4,41
;
declare @num int
select @num= 10;
;
with t1 as(
select a.id,CT/@num ctx from # a
	WHERE CT/@num>0
),t2 as(
select a.* from # a where ct<@num
union all select id,@num from (
	select ROW_NUMBER() OVER(partition BY a.id order by a.id)row,a.* 
		from t1 a, sys.sysobjects
)t where row<=ctx
union all select id,ct%@num from # a where ct>@num)
select * from t2 order by id,ct desc
drop table #
[/ 我用了个循环嵌套加IF判断,好像比较笨的法子。谢谢
撸大湿 2013-05-09
  • 打赏
  • 举报
回复
create table # (id int,CT INT)
insert #
select 1,1
union all select 2,2
union all select 3,24
union all select 4,41
;
declare @num int
select @num= 10;
;
with t1 as(
select a.id,CT/@num ctx from # a
	WHERE CT/@num>0
),t2 as(
select a.* from # a where ct<@num
union all select id,@num from (
	select ROW_NUMBER() OVER(partition BY a.id order by a.id)row,a.* 
		from t1 a, sys.sysobjects
)t where row<=ctx
union all select id,ct%@num from # a where ct>@num)
select * from t2 order by id,ct desc
drop table #

27,579

社区成员

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

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