求助!有关于对SQL数据处理的问题!

wwkjddyh 2010-09-25 01:37:47
我想对一组数据进行处理,原数据为:
a列 b列
1 2.8
2 3.6
我想通过sql语句能将此数据处理为:
a列 b列
1 1
1 1
1 0.8
2 1
2 1
2 1
2 0.6
也就是处理完的结果分组求和的值为处理完前数据的值!
是在sqlserver 2005环境下处理的!通过查询不知道能不能完成此数据处理?
...全文
88 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwkjddyh 2010-09-25
  • 打赏
  • 举报
回复
多谢!我已经成功处理了我需要的数据!再问一个问题!在查询可否直接将数据导出至csv文件?
[Quote=引用 2 楼 beirut 的回复:]
SQL code
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([a列] int,[b列] numeric(2,1))
insert [tb]
select 1,2.8 union all
select 2,3.6
go

-->测试开始
select a……
[/Quote]
chuifengde 2010-09-25
  • 打赏
  • 举报
回复
DECLARE @a TABLE(a INT,b DECIMAL(20,1))
INSERT @a SELECT 1, 2.8
union all SELECT 2, 3.6
UNION ALL SELECT 3, .3

SELECT a.a,CASE WHEN number<CEILING(a.b) THEN 1.0 ELSE a.b-FLOOR(a.b) END b FROM @a a ,MASTER..spt_values b
WHERE TYPE='P' AND number BETWEEN 1 AND CEILING (a.b)
ORDER BY a.a,number
--result
/*a b
----------- ------------------------
1 1.0
1 1.0
1 .8
2 1.0
2 1.0
2 1.0
2 .6
3 .3

(所影响的行数为 8 行)
*/
黄_瓜 2010-09-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bancxc 的回复:]
SQL code
while exists(select top 1 from tb where b>1)
begin
select top 1 @id=id,@a=a from tb where b>1
insert into tb(a,b) values(@a,1)
update tb set b=b-1 where id=@id
end
[/Quote]大王出山了,膜拜。v5
黄_瓜 2010-09-25
  • 打赏
  • 举报
回复
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([a列] int,[b列] numeric(2,1))
insert [tb]
select 1,2.8 union all
select 2,3.6
go

-->测试开始
select a列,1[b列]from [tb] t, master..spt_values s
where s.number between 1 and floor([b列])
and s.type='p'
union all
select a列,[b列]-floor([b列]) from [tb]
order by a列,b列 desc
/*
a列 b列
----------- ---------------------------------------
1 1.0
1 1.0
1 0.8
2 1.0
2 1.0
2 1.0
2 0.6

(7 行受影响)


*/
bancxc 2010-09-25
  • 打赏
  • 举报
回复
while exists(select top 1 from tb where b>1)
begin
select top 1 @id=id,@a=a from tb where b>1
insert into tb(a,b) values(@a,1)
update tb set b=b-1 where id=@id
end

22,207

社区成员

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

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