2013-09-16 66 views
-1

我有以下2個表:如何計算SQL中的2個表?

A (ID, No1, No2,D) 
B (ID, No3, No4,D) 

和表格數據被以下:

甲TABEL數據

ID = 1, No1 = 23, No2 = 45, D = 1 
ID = 2, No1 = 12, No2 = 5, D = 1 
ID = 3, No1 = 14, No2 = 4, D = 1 
ID = 4, No1 = 41, No2 = 12, D = 1 
ID = 5, No1 = 2, No2 = 25, D = 1 
ID = 6, No1 = 3, No2 = 96, D = 1 

乙表數據沒有任何數據

如果我通過以下SQL查詢

SELECT 
    ISNULL(SUM(dbo.A.No1), 0) AS No1Sum, 
    ISNULL(SUM(dbo.B.No3), 0) AS No3Sum, 
    ISNULL(SUM(dbo.A.No1), 0) - ISNULL(SUM(dbo.B.No3), 0) AS Diffrence 
FROM dbo.A LEFT 
OUTER JOIN dbo.B ON dbo.A.id = dbo.B.id 
GROUP BY dbo.B.No3 

那麼下面的數據是未來

No1Sum = 95, No3Sum = 0, Diffrence = 95 

但是當我加入WHERE條件和寫入查詢,則沒有數據在下面的查詢

SELECT 
    ISNULL(SUM(dbo.A.No1), 0) AS No1Sum, 
    ISNULL(SUM(dbo.B.No3), 0) AS No3Sum, 
    ISNULL(SUM(dbo.A.No1), 0) - ISNULL(SUM(dbo.B.No3), 0) AS Diffrence 
FROM dbo.A 
LEFT OUTER JOIN dbo.B ON dbo.A.id = dbo.B.id 
WHERE (dbo.A.D = 1) 
AND (dbo.B.D = 1) GROUP BY dbo.B.No3 

未來如果數據將precent兩個表中,然後結果顯示例如

No1Sum = 95, No3Sum = 10, Diffrence = 85, 

如果我給下面的查詢

SELECT 
    ISNULL(SUM(dbo.A.No1), 0) AS No1Sum, 
    ISNULL(SUM(dbo.B.No3), 0) AS No3Sum, 
    ISNULL(SUM(dbo.A.No1), 0) - ISNULL(SUM(dbo.B.No3), 0) AS Diffrence 
FROM dbo.A 
LEFT OUTER JOIN dbo.B 
    ON dbo.A.id = dbo.B.id 
    AND dbo.A.D = 1 
    AND dbo.B.D = 1 
GROUP BY dbo.B.No3 

然後下面的結果顯示

No1Sum = 570, No3Sum = 0, Diffrence = 570 

(它被求和6次爲一個表有6行)

因此,如何從兩個表,當兩個表中的數據的結果是計算準確的,但是當一個表有數據而B沒有數據時,結果不會到來。

在此先感謝

+3

實際上你的問題是什麼? –

+0

如果A表中有相同的ID,您只需要對錶B進行求和? –

+0

您能否澄清您的問題,您正在使用哪些特定數據以及您期待的具體結果? –

回答

2

此查詢不起作用的原因是因爲它只返回dbo.b.d爲1的記錄。但在您的查詢中它有時可以爲null。

SELECT 
    ISNULL(SUM(dbo.A.No1), 0) AS No1Sum, 
    ISNULL(SUM(dbo.B.No3), 0) AS No3Sum, 
    ISNULL(SUM(dbo.A.No1), 0) - ISNULL(SUM(dbo.B.No3), 0) AS Diffrence 
FROM 
     dbo.A LEFT OUTER JOIN dbo.B ON dbo.A.id = dbo.B.id 
WHERE 
     (dbo.A.D = 1) AND (dbo.B.D = 1) GROUP BY dbo.B.No3 

因此改變這一行

(dbo.A.D = 1) AND (dbo.B.D = 1) GROUP BY dbo.B.No3 

對此

(dbo.A.D = 1) AND (isnull(dbo.B.D, 1) = 1) GROUP BY dbo.B.No3 

ISNULL(阿根廷,默認值)設置爲ARG如果arg不爲空,否則,將它設置爲默認值。

+5

感謝它的工作....一個更多的問題..在我的問題中,我被添加只D = 1時的一個條件...如果要在兩個條件下從兩個表中獲取數據WHERE A.ID = 1 AND AD = 1 AND BD = 1 ...如何添加此條件..... –

+1

如果I正確理解你應該可以說'A.ID = 1和dbo.AD = 1 AND(isnull(dbo.BD,1)= 1)' – dcaswell

+5

是的,我的主要問題是我想計算2表。表A作爲購買和B表作爲銷售。在我的兩個表中有一個修復條件,D將1和一段時間它將改變爲2或以上,我想只使用那些記錄,其中D是1 ...現在我想要以我的平衡數量產品..當我做購買條目(表A)和沒有銷售條目(在表B)條件是相同的...所以根據你的答案它woking ..但因爲我想計算平衡,所以它會依賴於ID 。對於例A和B的Id有ProductId所以我想要的差異。 –

1

我認爲這樣做,即使第二個表有數據。

SELECT ISNULL(SUM(A.No1), 0) AS No1Sum , 
      ISNULL(SUM(B.No3), 0) AS No3Sum , 
      ISNULL(SUM(A.No1), 0) - ISNULL(SUM(B.No3), 0) AS Diffrence 
    FROM A 
      LEFT OUTER JOIN B ON A.id = B.id 

SELECT 
    ISNULL(SUM(dbo.A.No1), 0) AS No1Sum, 
    ISNULL(SUM(dbo.B.No3), 0) AS No3Sum, 
    ISNULL(SUM(dbo.A.No1), 0) - ISNULL(SUM(dbo.B.No3), 0) AS Diffrence 
FROM dbo.A 
LEFT OUTER JOIN dbo.B ON dbo.A.id = dbo.B.id 
WHERE (dbo.A.D = 1) 
AND (dbo.B.D = 1) GROUP BY dbo.B.No3 

此查詢不會獲取任何記錄,如果表B沒有任何數據,因爲這種狀態。

WHERE (dbo.A.D = 1) AND (dbo.B.D = 1)  

我想在連接上應用多個條件是開銷。

LEFT OUTER JOIN dbo.B 
    ON dbo.A.id = dbo.B.id 
    AND dbo.A.D = 1 --Overhead 
    AND dbo.B.D = 1 --Overhead 
+5

我也用過這個;;; LEFT OUTER JOIN dbo.B ON dbo.A.id = dbo.B.id AND dbo.AD = 1 --Overhead AND dbo.BD = 1 --Overhead –

+5

例如在表中有6行和B Tabels沒有任何行,那麼它將總結6次.....請閱讀我的問題的最後2段我已經提醒.. –