如何更新一张数据表内计算记录?

xxjltan 2016-03-29 03:46:50
declare @tb table(mc varchar(10),fc int,sr int,fp int)
insert @tb select'A1','100','510','10'
insert @tb select 'A1','210','510','10'
insert @tb select 'A1','200','510','10'
insert @tb select 'A2','320','330','5'
insert @tb select 'A2','10','330','5'
insert @tb select 'A3','33','33','3'
select * from @tb

/* 原表 sr 列为mc列各类型A1的总值510,A2总值330,A3总值33
mc fc sr fp
A1 100 510 10
A1 215 510 10
A1 200 510 10
A2 320 330 5
A2 10 330 5
A3 33 33 3
*/

要求得出如下结果

mc fc sr fp
A1 100 100 0
A1 215 210 10 ----这里MC列 A1类最大值215,即sr 除215哪行所有fc列A1值相加100+200=300,510-300=210这里算出210,10放到最大值215哪行
A1 200 200 0
A2 320 320 5 --320系=330-10=320,最后一个数5放到最大值哪一行
A2 10 10 0
A3 33 33 3

...全文
224 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xxjltan 2016-03-29
  • 打赏
  • 举报
回复
declare @tb table(mc varchar(10),fc int,sr int,fp int) declare @tb1 table(mc varchar(10),fc int,sr int,fp int,lx int) insert @tb select'A1','100','510','10' insert @tb select 'A1','210','510','10' insert @tb select 'A1','200','510','10' insert @tb select 'A2','320','330','5' insert @tb select 'A2','10','330','5' insert @tb select 'A3','33','33','3' insert @tb1 select *,rn=ROW_NUMBER()over(PARTITION by mc order by fc desc) from @tb update @tb1 set sr=fc,fp=0 where lx>1 select * from @tb1 现在就差一点点了,就是510,330哪两个数变成210和320就可以了, 210是等于=510-100-200 ,320=330-10,我表达不好,希望你多看几次就应该会明白我的意思的,这两个数一定要算出来才行
xxjltan 2016-03-29
  • 打赏
  • 举报
回复
引用 6 楼 spiritofdragon 的回复:
除非是你的总收入,本来就是随便填的......而不是通过计算汇总来的,那么可以这么写。
declare  @tb table(mc varchar(10),fc int,sr int,fp int)
insert @tb select'A1','100','510','10'
insert @tb select 'A1','215','510','10'
insert @tb select 'A1','200','510','10'
insert @tb select 'A2','320','330','5'
insert @tb select 'A2','10','330','5'
insert @tb select 'A3','33','33','3';
;
with t as 
(
select *
,ROW_NUMBER()over(PARTITION by mc order by fc desc) rn
,SUM(fc) over(PARTITION by mc) RealSr
,max(fc) over(PARTITION by mc) MaxFr
from @tb
)
update t set sr=case when rn>1 then fc else sr-(RealSr-MaxFr) end,fp=case when rn>1 then 0 else fp end 
select * from @tb


嗯,就是这个意思了,我再研究一下你的代码,谢谢!
spiritofdragon 2016-03-29
  • 打赏
  • 举报
回复
除非是你的总收入,本来就是随便填的......而不是通过计算汇总来的,那么可以这么写。
declare  @tb table(mc varchar(10),fc int,sr int,fp int)
insert @tb select'A1','100','510','10'
insert @tb select 'A1','215','510','10'
insert @tb select 'A1','200','510','10'
insert @tb select 'A2','320','330','5'
insert @tb select 'A2','10','330','5'
insert @tb select 'A3','33','33','3';
;
with t as 
(
select *
,ROW_NUMBER()over(PARTITION by mc order by fc desc) rn
,SUM(fc) over(PARTITION by mc) RealSr
,max(fc) over(PARTITION by mc) MaxFr
from @tb
)
update t set sr=case when rn>1 then fc else sr-(RealSr-MaxFr) end,fp=case when rn>1 then 0 else fp end 
select * from @tb


spiritofdragon 2016-03-29
  • 打赏
  • 举报
回复
引用 4 楼 xxjltan 的回复:
[quote=引用 2 楼 spiritofdragon 的回复:] 第2行到底是215还是210,如果是215那么fc应该是515...测试数据有问题? 再次...真心没看懂后面,反复加减在干嘛?我理解,不就是fp列在max放值,其他行为0就完了 ?那如果有相同的max行怎么办?
我想说倒数第二列是,名称A1类的总值,上面例了就510来分配名称A1类,哪个A1类最大一行数是215,把前面A1类的那两行fc的数相加,用510减去这两个的数510 -(100+200)=210填215哪一行。 [/quote] 你所谓的倒数第二列的逻辑(谁加谁谁减谁...),其实就是 =第二列 。至于为什么会有215和210不同,是因为,你的A汇总错误。如果A类第2行是215,那么类总收入应该是100+215+200=515。那么515-(100+200)=215不就是第2列么?所以,你求的结果倒数第二列=正数第二列,就可以。
xxjltan 2016-03-29
  • 打赏
  • 举报
回复
引用 2 楼 spiritofdragon 的回复:
第2行到底是215还是210,如果是215那么fc应该是515...测试数据有问题? 再次...真心没看懂后面,反复加减在干嘛?我理解,不就是fp列在max放值,其他行为0就完了 ?那如果有相同的max行怎么办?
我想说倒数第二列是,名称A1类的总值,上面例了就510来分配名称A1类,哪个A1类最大一行数是215,把前面A1类的那两行fc的数相加,用510减去这两个的数510 -(100+200)=210填215哪一行。
spiritofdragon 2016-03-29
  • 打赏
  • 举报
回复
忽略测试数据错误...
;with t as 
(
select *,rn=ROW_NUMBER()over(PARTITION by mc order by fc desc)
from @tb
)
update t set fp=0 where rn>1;
spiritofdragon 2016-03-29
  • 打赏
  • 举报
回复
第2行到底是215还是210,如果是215那么fc应该是515...测试数据有问题? 再次...真心没看懂后面,反复加减在干嘛?我理解,不就是fp列在max放值,其他行为0就完了 ?那如果有相同的max行怎么办?
xxjltan 2016-03-29
  • 打赏
  • 举报
回复

27,582

社区成员

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

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