我有一个 Ledger Table ,其中包含以下三列以及其他未包含的内容:
**gl_account id** | **gl_amount** | **GL_adjustmentAmount**
9500 | NULL | NULL
... | ... | ...
我有一个不同的调整表,上面有不同的列,除了两个:
**gl_account id** | **GL_adjustmentamount**
9500 | 289.84
9500 | 9.63
9500 | 13646.11
9500 | 835.31
9500 | -210
9500 | -1019.02
9500 | -200
我需要更新 Ledger Table 以包含所有7个新gl_adjustments,但它只包含其中一个值289.84。
这是我的代码。
UPDATE LedgerTable
SET [GL_adjustmentamount] = adjust.GL_adjustmentamount
FROM LedgerTable AS genLed
FULL OUTER JOIN AdjustmentTable AS adjust
ON genLed.gl_accountid = adjust.gl_accountid
答案 0 :(得分:1)
案例1:仅针对一个帐户进行更新
如果您只想在一个帐户中使用此功能,则可以使用此功能:
declare @AccountID INT
set @AccountID = 9500
update Ledger
set GL_adjustmentAmount = GL_adjustmentAmount + (select SUM(a.GL_AdjustmentAmount)
from Adjustment a
where a.GL_AccountID = @AccountID)
where GL_AccountID = @AccountID
请注意,此处必须指定accountId
。
案例2:所有帐户的更新
如果您希望这适用于所有帐户(应该&更可能是这种情况),那么您将需要更多"概括"查询类似的东西:
update Ledger
set GL_AdjustmentAmount = ISNULL(GL_AdjustmentAmount,0) + ISNULL(collatedAdjustments.adjustment, 0)
from Ledger ledger
inner join
(select a.GL_AccountID,
SUM(a.GL_AdjustmentAmount) as Adjustment
from Adjustment adjustments
group by adjustments.GL_accountID) as collated
on ledger.GL_accountID = collated.GL_accountID
这里有一些示例数据可供您测试:
CREATE TABLE Ledger(GL_AccountID int, GL_Amount int, GL_AdjustmentAmount int);
CREATE TABLE Adjustment(GL_AccountID int, GL_AdjustmentAmount int);
INSERT INTO Ledger(GL_AccountID,GL_Amount, GL_AdjustmentAmount) VALUES(9500, null, null);
INSERT INTO Ledger(GL_AccountID,GL_Amount, GL_AdjustmentAmount) VALUES(9600, null, null);
INSERT INTO Ledger(GL_AccountID,GL_Amount, GL_AdjustmentAmount) VALUES(9700, null, null);
INSERT INTO Ledger(GL_AccountID,GL_Amount, GL_AdjustmentAmount) VALUES(9800, null, null);
INSERT INTO Adjustment(GL_AccountID, GL_AdjustmentAmount) VALUES(9500, 289.84);
INSERT INTO Adjustment(GL_AccountID, GL_AdjustmentAmount) VALUES(9500, 9.63);
INSERT INTO Adjustment(GL_AccountID, GL_AdjustmentAmount) VALUES(9500, 13646.11);
INSERT INTO Adjustment(GL_AccountID, GL_AdjustmentAmount) VALUES(9500, 835.31);
INSERT INTO Adjustment(GL_AccountID, GL_AdjustmentAmount) VALUES(9500, -210);
INSERT INTO Adjustment(GL_AccountID, GL_AdjustmentAmount) VALUES(9500, -1019.02);
INSERT INTO Adjustment(GL_AccountID, GL_AdjustmentAmount) VALUES(9500, -200);
INSERT INTO Adjustment(GL_AccountID, GL_AdjustmentAmount) VALUES(9600, 29.84);
INSERT INTO Adjustment(GL_AccountID, GL_AdjustmentAmount) VALUES(9600, 29.63);
INSERT INTO Adjustment(GL_AccountID, GL_AdjustmentAmount) VALUES(9600, 16646.11);
INSERT INTO Adjustment(GL_AccountID, GL_AdjustmentAmount) VALUES(9600, 335.31);
INSERT INTO Adjustment(GL_AccountID, GL_AdjustmentAmount) VALUES(9600, -1210);
INSERT INTO Adjustment(GL_AccountID, GL_AdjustmentAmount) VALUES(9600, -2019.02);
INSERT INTO Adjustment(GL_AccountID, GL_AdjustmentAmount) VALUES(9600, -1200);
INSERT INTO Adjustment(GL_AccountID, GL_AdjustmentAmount) VALUES(9700, 2239.02);
INSERT INTO Adjustment(GL_AccountID, GL_AdjustmentAmount) VALUES(9700, 1400);
INSERT INTO Adjustment(GL_AccountID, GL_AdjustmentAmount) VALUES(9800, 4121.02);
INSERT INTO Adjustment(GL_AccountID, GL_AdjustmentAmount) VALUES(9800, 1234);
此查询也会将之前的任何调整(在ledger
表中)纳入等式中。
希望这有帮助!!!
答案 1 :(得分:0)
select * into LedgerTable
from Adjustment Table