2013-08-29 75 views
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 

我要添加標籤LE-2 Products_Count表1 Products_Count每個Sale_RepID在表1

能否請您用SQL查詢幫助嗎?

我的數據庫是MS SQL SERVER

+0

哪個你的數據庫? MS SQL Server,Oracle,MySql等? – TechDo

+0

我的數據庫是MS SQL SERVER – Subash

回答

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 
+0

Hi Techdo感謝您的回覆。我嘗試了你所建議的方式。查詢正在成功執行,沒有任何錯誤。但它沒有更新表中的任何內容。你能提出建議嗎? – Subash

+0

哪個是您的數據庫服務器? – TechDo

+0

我的數據庫是MS SQL SERVER – Subash

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 

要更新表1的總,加來自表2的總數:

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的值是而不是累積的:它們是可確定的計算值,在這種情況下將只是總和,而不是現有值加上的總和。您的設計意味着查詢只能執行一次。之後,你會反覆重新添加表2中的總數。

考慮重新設計表格,從表1表2有直和,即:

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; 
+0

你能否請建議一個更新聲明 – Subash

+0

@Subash OK OK(對不起 - 我誤解了你的問題) – Bohemian

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

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 
+0

Hi..Ca你給我提供一個更新聲明...我想更新表-1 – Subash

+0

檢查更新編號 –

+0

嗨Mhasan,謝謝你的答覆。我嘗試了你建議的方式,但它不工作。是否通過添加其他表中的值來更新表格? – Subash

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 
相關問題