来自两个表的总和如果不存在则插入

时间:2012-12-11 08:57:06

标签: sql sql-server-2008 sum full-outer-join

我有一种情况,我希望总结两个表之间的差异。问题是第二个表中可以存在一行,然后我想将其作为新行插入。

SELECT T1.seller, T1.code, T1.amount - T2.amount

查看图片以获得解释

LINK

4 个答案:

答案 0 :(得分:2)

 DECLARE @T1 TABLE(
    seller VARCHAR(10),
    code VARCHAR(3) NULL,
    amount MONEY
)

 DECLARE @T2 TABLE(
    seller VARCHAR(10),
    code VARCHAR(3) NULL,
    amount MONEY
)

INSERT INTO @T1 VALUES
('VL',NULL,1),
('VL','317',70005.6)

INSERT INTO @T2 VALUES
('VL',NULL,0.5),
('VL','500',4450)

SELECT seller,code,SUM(amount) [amount] FROM 
(
SELECT * FROM @T1
UNION ALL
SELECT seller,code,-amount as amount FROM @T2
) T
GROUP BY seller,code

答案 1 :(得分:1)

你需要做一个完整的外部联接 - 然后加以总结。如果你只运行内部查询,你将获得每个可能的行组合(在t1中是独占的,在t1和t2中都存在,在t2中是exlclusive) - 然后你将它组合起来并做总和。

SELECT  Seller ,
        Code ,
        SUM(Tab1_amt - Tab2_amt) AS Amount
FROM    ( SELECT    COALESCE(tab1.Seller, tab2.Seller) AS Seller ,
                    COALESCE(tab1.code, tab2.code) AS Code ,
                    COALESCE(tab1.amount, 0) AS tab1_amt ,
                    COALESCE(tab2.amount, 0) AS tab2_amt
          FROM      tab1
                    FULL OUTER JOIN tab2 ON tab1.seller = tab2.seller
                                            AND tab1.code = tab2.code
        ) AS Tbl
GROUP BY Seller ,
        Code

请参阅SQLFiddle Demo

答案 2 :(得分:1)

我认为您需要FULL JOIN(除非您确实希望将行插入第一个表格)

SELECT  COALESCE(t1.Seller, t2.Seller) AS Seller,
        COALESCE(t1.Code, t2.Code) AS Code,
        COALESCE(t1.Amount, 0) - COALESCE(t2.Amount, 0) AS Amount
FROM    Table1 t1
        FULL JOIN Table2 t2
            ON t1.Seller = t2.Seller
            AND COALESCE(t1.Code, 0) = COALESCE(t2.Code, 0);

如果确实需要插入表1的行,则需要执行2次操作,首先插入,然后选择:

INSERT Table1 (Seller, Code, Amount)
SELECT  t2.Seller, t2.Code, 0 AS Amount
FROM    Table2 t2
WHERE   NOT EXISTS
        (   SELECT  1
            FROM    Table1 t1
            WHERE   t1.Seller = t2.Seller
            AND     COALESCE(t1.Code, 0) = COALESCE(t2.Code, 0)
        );

SELECT  t1.Seller, 
        t1.Code, 
        t1.Amount - COALESCE(t2.Amount, 0) AS Amount
FROM    Table1 t1
        LEFT JOIN Table2 t2
            ON t1.Seller = t2.Seller
            AND COALESCE(t1.Code, 0) = COALESCE(t2.Code, 0);

修改

如果每个表中的行都不是unqiue而你需要对它们求和,那么你需要在子查询中进行求和,因为JOIN将引入交叉连接:

考虑这个数据

Table1
Seller  Code    Amount
VL      500     10
VL      500     20

Table2
Seller  Code    Amount
VL      500     30
VL      500     5

当你加入这个时,你会得到:

t1.Seller   t1.Code t1.Amount   t2.Seller   t2.Code t2.Amount
VL          500     10          VL          500     30
VL          500     10          VL          500     5
VL          500     20          VL          500     30
VL          500     20          VL          500     5

差值的总和是-10而不是-5。

SELECT  COALESCE(t1.Seller, t2.Seller) AS Seller,
        COALESCE(t1.Code, t2.Code) AS Code,
        COALESCE(t1.Amount, 0) - COALESCE(t2.Amount, 0) AS Amount
FROM    (   SELECT  Seller, Code, SUM(Amount) AS Amount
            FROM    Table1 
            GROUP BY Seller, Code
        ) t1
        FULL JOIN 
        (   SELECT  Seller, Code, SUM(Amount) AS Amount
            FROM    Table2 
            GROUP BY Seller, Code
        ) t2
            ON t1.Seller = t2.Seller
            AND COALESCE(t1.Code, 0) = COALESCE(t2.Code, 0);

编辑2

Daniel's answer中的UNION方法的效果会比完全加入好得多:

SELECT  Seller, Code, Amount = SUM(Amount)
FROM    (   SELECT  Seller, Code, Amount
            FROM    Table1
            UNION
            SELECT  Seller, Code, -Amount
            FROM    Table2
        ) t
GROUP BY Seller, Code

答案 3 :(得分:1)

这样的东西?你不知道要插入“因此tableX”中的表格

insert into tableX (seller,code,amount) values (T1.seller, T1.code, T1.amount -T2.amount)
select count(*) from table2 having count(*) < 1

请注意,在插入应用过滤器后,将过滤器设置为select语句 你没有指定你需要什么类型的SQL,所以我不能说这是否有效