![](/img/trans.png)
[英]Find a user_id from string where id is one digit or 2 digit using SQL Server query
[英]Returning sum in one table in one row from a series of records per user_ID
假設主表具有10列與Amount
列,它必須形成一個第二表,該更新DepositTable
,與deposit
柱
MainTable id 是titheID
,並且是唯一的,因此不允許有多個相同的值。
DepositTable
可以在不同存款日期后的幾行上具有相同的titheID
。
我試過了
SELECT titheID, SUM(deposit) FROM DepositTable GROUP BY titheID
它適用於存款表,但如何使用每個 titheID 的結果更新 MainTable Amount 列,這是我的難題。
由於 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
我對嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.