0

您能帮我解决以下问题吗?

我有一张如下表。

表格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

我想为表 1中的每个 Sale_RepID将表 2 Products_Count添加到1 Products_Count

您能帮忙处理 SQL 查询吗?

我的数据库是 MS SQL SERVER

4

4 回答 4

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
于 2013-08-29T05:30:05.867 回答
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
于 2013-08-29T04:43:11.283 回答
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 上执行的此更新语句的实时演示。


请注意,这是一个不寻常的查询。通常,此类非规范化不是累积的:它们是可确定的计算值,在这种情况下,它只是简单的总和,而不是现有值加上总和。您的设计意味着查询只能执行一次。之后,您将反复从 table2 重新添加总数。

考虑重新设计您的表格,以便在 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;
于 2013-08-29T04:49:07.633 回答
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
于 2013-08-29T08:07:20.643 回答