同表更新多列统计数据的问题

lphy 2008-04-15 01:32:58
UPDATE [Table1] SET b = (select sum(b) from [Table1] where c=1 and a<33) where a=35;
这样更新一列是没问题
我要同时更新多列,该怎么写?
b=sum(b),c=sum(c),d=sum(d)....
...全文
88 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
-狙击手- 2008-04-15
  • 打赏
  • 举报
回复
UPDATE a
SET
b = b.b,
c = b.c,
d = b.d
from [Table1] a,
(select 35 as a ,sum(b) b,sum(c) c, sum(d) d from [Table1] where c=1 and a <33) b
where a.a = b.a and a=35
kakajya 2008-04-15
  • 打赏
  • 举报
回复
貌似没有。
UPDATE [Table1] SET
b = (select sum(b) from [Table1] where c=1 and a <33),
c = (select sum(c) from [Table1] where c=1 and a <33),
d = (select sum(d) from [Table1] where c=1 and a <33)
where a=35

不能实现你的要求吗?
lphy 2008-04-15
  • 打赏
  • 举报
回复
晕,有没有更简洁的语句了?
谢谢大家支持!
zccmy22 2008-04-15
  • 打赏
  • 举报
回复
UPDATE [Table1] SET
b = (select sum(b) from [Table1] where c=1 and a <33),
c = (select sum(c) from [Table1] where c=1 and a <33),
d = (select sum(d) from [Table1] where c=1 and a <33)
where a=35

至于子查询,只要记得结果是结果,不是集合就可以用的。无论多少个都可以的。
dawugui 2008-04-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 Limpire 的回复:]
UPDATE [Table1] SET
b = (select sum(b) from [Table1] where c=1 and a <33),
c = (select sum(c) from [Table1] where c=1 and a <33),
...
n = (select sum(n) from [Table1] where c=1 and a <33)
where a=35
[/Quote]
同意.

最后的;可以不要.
Limpire 2008-04-15
  • 打赏
  • 举报
回复
UPDATE [Table1] SET
b = (select sum(b) from [Table1] where c=1 and a <33),
c = (select sum(c) from [Table1] where c=1 and a <33),
...
n = (select sum(n) from [Table1] where c=1 and a <33)
where a=35;
wangxuelid 2008-04-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 Limpire 的回复:]
UPDATE [Table1] SET
b = (select sum(b) from [Table1] where c=1 and a <33),
c = (select sum(c) from [Table1] where c=1 and a <33),
...
n = (select sum(n) from [Table1] where c=1 and a <33)
where a=35;
[/Quote]
lphy 2008-04-15
  • 打赏
  • 举报
回复
谢谢各位的参与~!
lphy 2008-04-15
  • 打赏
  • 举报
回复
搞定了
感谢happyflystone的启发,最终SQL:
UPDATE aa
SET
b = bb.b,
c = bb.c,
d = bb.d
from [Table1] aa,
(select sum(b) as a ,sum(b) b,sum(c) c,sum(d) d from [Table1] where c=1 and a <33) bb
where aa.a=35
lphy 2008-04-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 happyflystone 的回复:]
SQL codeUPDATE a
SET
b = b.b,
c = b.c,
d = b.d
from [Table1] a,
(select 35 as a ,sum(b) b,sum(c) c, sum(d) d from [Table1] where c=1 and a <33) b
where a.a = b.a and a=35
[/Quote]
列名 'a' 不明确。
lphy 2008-04-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 kakajya 的回复:]
貌似没有。
UPDATE [Table1] SET
b = (select sum(b) from [Table1] where c=1 and a <33),
c = (select sum(c) from [Table1] where c=1 and a <33),
d = (select sum(d) from [Table1] where c=1 and a <33)
where a=35

不能实现你的要求吗?
[/Quote]
可以,自己我自己也会写,想征求更简洁的写法:)

34,588

社区成员

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

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