アカウントソートの何か:
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);
: 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
ここではいくつかのいくつかのサンプルデータを使用すると、反対にこれをテストすることができます
このクエリでは、以前の調整値(ledger
テーブル内)も同様に式に反映されます。
期待どおりの結果が得られましたか?あなたは値を「合計」しようとしていますか? – sgeddes
元帳テーブルのすべての調整の合計で1つのレコードを作成しますか?もしそうなら、あなたはcte/subqueryで集計してから更新する必要があります。そうでなければ調整ごとに1レコードが必要な場合は、 'UPDATE'ではなくINSERTする必要があります。 –
私はsql 2008を使用しています。私は当初、元帳のすべてのGL_adjustedamountを持っていたかったのですが、私は合計値が最高になると思います。 :) – Ace