把表里面某个字段的值全部改为其它值,然后把相同的数量累加在一起(注意有三个主键)~~我在线等!!!!

zxz107 2005-01-06 11:21:56
我要实现这样的功能,当a1=3的,全部修改为a1=1,表里有三个主键(a1,a2,a3),当他们相同时,就把他们的数量(sl)相加,成为一个没有a1=2的表,但是如果它和其它行值的a2,a3相等的话,就把它们的数量加起来
表,a1,a2,a3,a4,a5,a6,sl三个主键(a1,a2,a3)
例:a1, a2, a3, a4, a5, sl
1 2 2 3 3 20
2 2 2 4 3 60
1 1 2 2 6 30
2 1 2 5 6 60
2 1 1 3 4 30
3 2 2 2 3 50
我要实现的结果:
例:a1, a2, a3, a4, a5, sl
1 2 2 3 3 80(20+60)//把上面的第二行的a1改为1,把第一行的数量加60
1 1 2 2 6 90(30+60)//把上面的第四行的a1改为1,把第三行的数量加60
1 1 1 3 4 30//因为它的a2和a3和其它行的不相等,只把他的a1改为1
3 2 2 2 3 50
希望得到你的帮助!!!!
谢谢!!!


...全文
141 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zxz107 2005-01-07
  • 打赏
  • 举报
回复
补充下,a4和a5都不要管的,只要关注三个主键和数量就可以了!不知道大家是否明白我的意思。。。
zxz107 2005-01-07
  • 打赏
  • 举报
回复
我不是只做查询,后面的记录是数据库里面的记录~这里面应该包含修改和删除,而且数据库中不只这几条记录,有上千条
zxz107 2005-01-07
  • 打赏
  • 举报
回复
谢谢谢谢!!不过我电脑坏了,现在还在搞。。。晕了。。
njz168 2005-01-07
  • 打赏
  • 举报
回复
因为我的电脑没有安装mssql。呵呵
njz168 2005-01-07
  • 打赏
  • 举报
回复
在SYBASE数据库下测试通过。
mssql请把这句换一下:while (@@sqlstatus=0)/*mssql:(@@fetch_status=0)*/
njz168 2005-01-07
  • 打赏
  • 举报
回复
CREATE PROCEDURE dbo.u_test1;1

AS
begin
declare @a1 int,@a2 int ,@a3 int,@sl int
declare my_cusor cursor for
select a1,a2,a3,sl from test where a1 =2
open my_cusor
fetch my_cusor into @a1,@a2,@a3,@sl
while (@@sqlstatus=0)
begin
if exists(select a1 from test where a1=1 and a2=@a2 and a3=@a3)
begin
update test set sl=sl+@sl where a1=1 and a2=@a2 and a3=@a3
delete from test where a1=@a1 and a2=@a2 and a3=@a3
end
else
update test set a1=1 where a1=@a1 and a2=@a2 and a3=@a3
fetch my_cusor into @a1,@a2,@a3,@sl
end
deallocate cursor my_cusor
end
canyqf 2005-01-06
  • 打赏
  • 举报
回复
select a1,a2,a3,max(a4),max(a5),sum(sl)
from (
select
case a1 when 3 then 1 else a1 end a1,
a2,a3,
case a1 when 3 then null else a4 end a4,
case a1 when 3 then null else a5 end a5,
sl
from table
) tmp
group by a1,a2,a3

34,590

社区成员

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

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