SQL更新查询中的聚合函数?

时间:2022-01-26 22:51:54

I'm trying to set the value in one table to the sum of the values in another table. Something along these lines:

我试图将一个表中的值设置为另一个表中的值之和。沿着这些线路:

UPDATE table1
SET field1 = SUM(table2.field2)
FROM table1
INNER JOIN table2 ON table1.field3 = table2.field3
GROUP BY table1.field3

Of course, as this stands, it won't work - SET doesn't support SUM and it doesn't support GROUP BY.

当然,就像这样,它不会起作用——SET不支持SUM,它不支持GROUP BY。

I should know this, but my mind's drawing a blank. What am I doing wrong?

我应该知道这一点,但我的脑子一片空白。我做错了什么?

5 个解决方案

#1


122  

UPDATE t1
SET t1.field1 = t2.field2Sum
FROM table1 t1
INNER JOIN (select field3, sum(field2) as field2Sum
   from table2
  group by field3) as t2
on t2.field3 = t1.field3  

#2


7  

Use:

使用:

UPDATE table1
   SET field1 = (SELECT SUM(t2.field2) 
                   FROM TABLE2 t2 
                  WHERE t2.field3 = field2)

#3


5  

Or you could use a mix of J*s and OMG Ponies answers:

或者你也可以混合使用J*s和OMG小马的回答:

UPDATE table1
   SET field1 = (SELECT SUM(field2)
                   FROM table2 AS t2
                  WHERE t2.field3 = t1.field3)
  FROM table1 AS t1

#4


3  

A good situation to use CROSS APPLY

使用交叉应用的好情况

UPDATE t1
   SET t1.field1 = t2.field2Sum
  FROM table1 t1
 CROSS APPLY (SELECT SUM(field2) as field2Sum
                FROM table2 t2
               WHERE t2.field3 = t1.field3) AS t2

#5


0  

I know the question is tagged SQL Server but be careful with UPDATE with JOIN if you are using PostgreSQL. @J*s answer won't work :

我知道这个问题被标记为SQL Server,但是如果您使用PostgreSQL,请小心使用JOIN进行更新。@J*s回答无效:

UPDATE t1
SET t1.field1 = t2.field2Sum
FROM table1 t1
INNER JOIN (...) as t2
on t2.field3 = t1.field3  

You will have to adapt it to :

你必须使它适应:

UPDATE table1 t1
SET t1.field1 = t2.field2Sum
FROM (...) as t2
WHERE t2.field3 = t1.field3  

See parameter from_list is the doc to get why FROM is considered by PostgreSQL as a self-join : https://www.postgresql.org/docs/9.5/static/sql-update.html#AEN89239

参见参数from_list是获取原因的doc, PostgreSQL将其视为自连接:https://www.postgresql.org/docs/9.5/static/sql-update.html#AEN89239

#1


122  

UPDATE t1
SET t1.field1 = t2.field2Sum
FROM table1 t1
INNER JOIN (select field3, sum(field2) as field2Sum
   from table2
  group by field3) as t2
on t2.field3 = t1.field3  

#2


7  

Use:

使用:

UPDATE table1
   SET field1 = (SELECT SUM(t2.field2) 
                   FROM TABLE2 t2 
                  WHERE t2.field3 = field2)

#3


5  

Or you could use a mix of J*s and OMG Ponies answers:

或者你也可以混合使用J*s和OMG小马的回答:

UPDATE table1
   SET field1 = (SELECT SUM(field2)
                   FROM table2 AS t2
                  WHERE t2.field3 = t1.field3)
  FROM table1 AS t1

#4


3  

A good situation to use CROSS APPLY

使用交叉应用的好情况

UPDATE t1
   SET t1.field1 = t2.field2Sum
  FROM table1 t1
 CROSS APPLY (SELECT SUM(field2) as field2Sum
                FROM table2 t2
               WHERE t2.field3 = t1.field3) AS t2

#5


0  

I know the question is tagged SQL Server but be careful with UPDATE with JOIN if you are using PostgreSQL. @J*s answer won't work :

我知道这个问题被标记为SQL Server,但是如果您使用PostgreSQL,请小心使用JOIN进行更新。@J*s回答无效:

UPDATE t1
SET t1.field1 = t2.field2Sum
FROM table1 t1
INNER JOIN (...) as t2
on t2.field3 = t1.field3  

You will have to adapt it to :

你必须使它适应:

UPDATE table1 t1
SET t1.field1 = t2.field2Sum
FROM (...) as t2
WHERE t2.field3 = t1.field3  

See parameter from_list is the doc to get why FROM is considered by PostgreSQL as a self-join : https://www.postgresql.org/docs/9.5/static/sql-update.html#AEN89239

参见参数from_list是获取原因的doc, PostgreSQL将其视为自连接:https://www.postgresql.org/docs/9.5/static/sql-update.html#AEN89239