SQL 聚合可能不会出现在 UPDATE 语句的集合列表中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14738900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
An aggregate may not appear in the set list of an UPDATE statement
提问by Kuthay Gümü?
UPDATE [silverdb01].[dbo].[info]
SET [FM] = SUM(a.[MONDAY] - b.[QUOTA])
FROM [silverdb01].[dbo].[info] a, [silverdb01].[dbo].[quota] b
WHERE a.[WORK_TYPE]='IN' AND a.[NAME]='KUTHAY'
When I run this I get the following error:
当我运行它时,我收到以下错误:
An aggregate may not appear in the set list of an UPDATE statement.
聚合可能不会出现在 UPDATE 语句的集合列表中。
Any ideas?
有任何想法吗?
回答by Lance
I am guessing that (as other's have pointed out) you don't really want a cartesian on this update so I have added an "id" to the query so you will have to do some modification but this might get you on the right path
我猜(正如其他人指出的那样)你真的不想要这个更新的笛卡尔所以我在查询中添加了一个“id”,所以你必须做一些修改,但这可能会让你走上正确的道路
;with temp as (
select a.id, SUM(a.pazartesi - b.kota) as newTotal
from [asgdb01].[dbo].[info] a join [asgdb01].[dbo].[kota] b
on a.id = b.id
where a.work_type='in' and a.name='alp' )
update a
set fm = t.newTotal
from [asgdb01].[dbo].[info] a join temp t on a.id = t.id