2016-07-25 5 views
0

で一つのテーブルを更新しようと、私は簡潔にするために含まれていない他の人と一緒に、以下の3つの列を持つ元帳テーブルあります別のテーブル

**gl_account id** | **gl_amount** | **GL_adjustmentAmount** 
    9500   |  NULL  |  NULL 
    ...   | ...   |  ... 

を私は異なる列で異なる調整テーブルを持っています上記2点を除いて、私はすべての7つの新しいgl_adjustmentsが、私を含めて元帳テーブルを更新する必要が

**gl_account id** | **GL_adjustmentamount** 
    9500   |   289.84 
    9500   |   9.63 
    9500   |   13646.11 
    9500   |   835.31 
    9500   |   -210 
    9500   |   -1019.02 
    9500   |   -200 

tは、289.84の値の1つのみを含みます。

ここに私のコードです。

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

期待どおりの結果が得られましたか?あなたは値を「合計」しようとしていますか? – sgeddes

+0

元帳テーブルのすべての調整の合計で1つのレコードを作成しますか?もしそうなら、あなたはcte/subqueryで集計してから更新する必要があります。そうでなければ調整ごとに1レコードが必要な場合は、 'UPDATE'ではなくINSERTする必要があります。 –

+0

私はsql 2008を使用しています。私は当初、元帳のすべてのGL_adjustedamountを持っていたかったのですが、私は合計値が最高になると思います。 :) – Ace

答えて

1

CASE 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がする必要がありますここで指定します。

CASE 2:すべての更新は、あなたが、これはすべてのアカウント(&可能性が高い場合になります必要があります)のために仕事をしたい場合は、あなたがより多くの「一般」のクエリが必要になります

アカウントソートの何か:

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テーブル内)も同様に式に反映されます。

+0

OPは口座IDで渡されていないことがわかります。 – sgeddes

+0

'Ledger.GL_AdjustmentAmount'も単に置き換えるのではなく、組み込みたいのでしょうか?すなわち 'set GL_AdjustmentAmount = GL_AdjustmentAmount + ...' – Glenn

+0

@Glennそれは良い点です。それに応じて答えを更新しました。 –

0
select * into LedgerTable 
from Adjustment Table