2014-11-24 63 views
0

您好我試圖從兩個不同的表中創建一個視圖有一個額外的列是Table1.Price1 - Table2.Price2的總和。沒有額外的列視圖是:SQL創建一個額外的總和列的視圖

Create View testview AS (
SELECT t1.ID,t1.Price1,t2.Price2 FROM 
Table1 t1 
LEFT JOIN 
Table2 t2 
ON 
t1.ID = t2.ID 
); 

任何幫助將不勝感激,謝謝。

下面是什麼樣的看法,會象表示:

ID | Table1.Price1 | Table2.Price2 | Total | 
--------------------------------------- 
1 | 15.00  | 5.00 | 10.00 | 

2 | 10.00  | 2.50 | 7.50  | 
+2

'選擇.... T1.Price2-T2.Price2作爲總FROM 表1 T1 LEFT JOIN 表2 T2 ON t1.ID = T2。 ID' – 2014-11-24 16:47:53

回答

2

試試這個:

Create View testview AS 
(SELECT t1.ID,t1.Price1,t2.Price2, (t1.Price1 - t2.Price2) as difference 
FROM Table1 t1 
LEFT JOIN Table2 t2 
ON t1.ID = t2.ID); 
0

只需添加您的SUM列,例如:

Create View testview AS (
SELECT t1.ID,t1.Price1,t2.Price2, t1.Price1-t2.Price2 AS [SUM] FROM 
Table1 t1 
LEFT JOIN 
Table2 t2 
ON 
t1.ID = t2.ID 
); 
0

你可以使用+, - ,*,/運算符添加兩列,如下所示:

Create View testview AS (
    SELECT t1.ID,t1.Price1,t2.Price2, t1.Price1+t2.Price2 as total FROM 
    Table1 t1 
    LEFT JOIN 
    Table2 t2 ON t1.ID = t2.ID 
); 

這將導致所需的表結構