从每 user_ID 的一系列记录中返回一行 table 中的总和
Returning sum in one table in one row from a series of records per user_ID
假设 Main Table 有 10 列和 Amount
列,必须从第二个 table、DepositTable
和 deposit
更新专栏
MainTable id 是 titheID
,并且是唯一的,因此不允许有多个相同的值。
DepositTable
可以在不同存款日期后的几行中具有相同的 titheID
。
我试过了
SELECT titheID, SUM(deposit)
FROM DepositTable
GROUP BY titheID
它对存款有效Table,但如何用每个 titheID 的结果更新主Table 金额列是我的难题。
由于 MainTable 中 titheID 的唯一状态,它会抛出约束异常。
请帮忙
如果我没理解错的话,您正在寻找类似的东西:
UPDATE MT
SET MT.Amount = Sec.Total
FROM MaiTable MT INNER JOIN (SELECT titheID, SUM(deposit) Total FROM DepositTable GROUP BY titheID) Sec ON MT.titheID = Sec.titheID
我说得对吗?
假设 Main Table 有 10 列和 Amount
列,必须从第二个 table、DepositTable
和 deposit
更新专栏
MainTable id 是
titheID
,并且是唯一的,因此不允许有多个相同的值。DepositTable
可以在不同存款日期后的几行中具有相同的titheID
。我试过了
SELECT titheID, SUM(deposit) FROM DepositTable GROUP BY titheID
它对存款有效Table,但如何用每个 titheID 的结果更新主Table 金额列是我的难题。
由于 MainTable 中 titheID 的唯一状态,它会抛出约束异常。
请帮忙
如果我没理解错的话,您正在寻找类似的东西:
UPDATE MT
SET MT.Amount = Sec.Total
FROM MaiTable MT INNER JOIN (SELECT titheID, SUM(deposit) Total FROM DepositTable GROUP BY titheID) Sec ON MT.titheID = Sec.titheID
我说得对吗?