通过从其他表添加来更新表值

时间:2013-08-29 04:31:01

标签: sql sql-server

你可以帮我解决下面的问题吗?

我有一张如下表格。

表格-1

Sales_RepID--                Name--           Products_Count

   1--                        ABC--                   2

   2--                        XYZ--                   4

   3--                        XXX--                   3

表格-2

Order_ID--  Sales_RepID--   Products_Count

1001--               2  --              2

1002--               1  --              1

1003--               2  --          1

1004--               3  --          3

1005--               2  --              2

表格 - 1结果

Sales_RepID,    --Name, --Products_Count

1-- ABC --3

2-- XYZ --9

3-- XXX --6

我想为 table-1

中的每个Sale_RepID将table-2 Products_Count 添加到Table-1 Products_Count

你能帮忙解决SQL查询吗?

我的数据库是MS SQL SERVER

4 个答案:

答案 0 :(得分:0)

对于MS SQL Server,请尝试:

UPDATE T
SET T.Products_Count=T.Products_Count+x.VSum
FROM Table1 T JOIN
(
    SELECT DISTINCT 
        Sales_RepID, 
        SUM(Products_Count) OVER (PARTITION BY Sales_RepID) VSum 
    FROM 
        Table2
)x ON T.Sales_RepID=x.Sales_RepID

答案 1 :(得分:0)

作为选择输出:

select
    t1.Sales_RepID,
    t1.Name,
    t1.Products_Count + sum(t2.Products_Count)
from table1 t1
left join table2 t2 on t2.Sales_RepID = t1.Sales_RepID

要更新table1中的总数,请从table2中添加总计:

update table1 set
Products_Count = Products_Count + (
    select sum(Products_Count)
    from table2
    where Sales_RepID = table1.Sales_RepID)

这些查询适用于所有SQL方言。

MS SQL Server有一种使用连接进行更新的特殊语法,它的性能比上面的通用更新语法要好得多:

update t1 set
t1.Products_Count = t1.Products_Count + t2.Products_Count
from table1 t1
join (select Sales_RepID, sum(Products_Count) Products_Count
      from table2
      group by Sales_RepID) t2
      on t2.Sales_RepID = t1.Sales_RepID;

查看在SQLFiddle上执行的此更新语句的live demo


请注意,这是一个不寻常的查询。通常,此类denormalized累积:它们是可确定的计算值,在这种情况下,wold只是总和,而不是现有值 plus 和。您的设计意味着查询只能执行一次。之后,您将反复重新添加表2中的总数。

考虑重新设计表格以获得table1中table2的直接和,即:

update t1 set
t1.Products_Count = t2.Products_Count
from table1 t1
join (select Sales_RepID, sum(Products_Count) Products_Count
      from table2
      group by Sales_RepID) t2
      on t2.Sales_RepID = t1.Sales_RepID;

答案 2 :(得分:0)

create table table1(sales_repId int,name varchar(10),product_count int);
create table table2(order_id int,sales_repId int,product_count int);


insert into table1 values(1,'ABC',2);
insert into table1 values(2,'XYZ',4);
insert into table1 values(3,'XXX',3);



insert into table2 values(1001,2,2);
insert into table2 values(1002,1,1);
insert into table2 values(1003,2,1);
insert into table2 values(1004,3,3);
insert into table2 values(1005,2,2);



select a.sales_repid,name,a.product_count+sum(b.product_count)
      from table1 a 
      inner join table2 b
      on a.sales_repid=b.sales_repid
group by  a.sales_repid,name,a.product_count
order by a.sales_repid

<强>更新

update table1
set product_count =  netProduct
from (
select a.sales_repid,name,a.product_count+sum(b.product_count) as netProduct
      from table1 a 
      inner join table2 b
      on a.sales_repid=b.sales_repid
group by  a.sales_repid,name,a.product_count
) z
inner join table1 x
on z.sales_repid=x.sales_repid

答案 3 :(得分:0)

尝试这个

DECLARE @TABLE1 AS TABLE( Sales_RepID INT,Name   VARCHAR(100), Products_Count int)
DECLARE @TABLE2 AS TABLE( Order_ID INT,Sales_RepID INT, Products_Count int)

INSERT INTO @TABLE1
VALUES(1,'ABC',2),(2,'XYZ',4),(3,'XXX',3)


INSERT INTO @TABLE2
VALUES(1001,2,2),(1002,1,1),(1003,2,1),(1004,3,3),(1005,2,2)

SELECT * FROM @TABLE1
SELECT * FROM @TABLE2

UPDATE T1
SET     T1.Products_Count = T1.Products_count + total
FROM @TABLE1 T1
CROSS APPLY (
SELECT total= sum(Products_count)
FROM @Table2 T2
WHERE T1.Sales_RepID =t2.Sales_RepID ) Z