2012-05-28 62 views
-1

我有以下SQL:SQL爲什麼這個連接不起作用?

SELECT tab4.IRSpot as IRSpot, tab4.IRVol AS MaxPlauShift, tab4.VegElement AS MaxPlauLoss FROM 
    (SELECT tab1.IRSpot, MIN(tab1.VegaElement*Probability) AS PlausibleLoss FROM 
     (SELECT IRSpot, IRVol, VegaElement 
     FROM SEL_VegaElementMatrix()) tab1 
    JOIN 
     (SELECT IRSpot, IRVol, Probability 
     FROM Table_Name_2) tab2 
    ON tab1.IRSpot = tab2.IRSpot AND tab1.IRVol = tab2.IRVol 
    GROUP BY tab1.IRSpot) tab3 
JOIN 
    (SELECT IRSpot, IRVol, VegaElement 
    FROM SEL_VegaElementMatrix()) tab4 
ON tab3.PlausibleLoss = tab4.VegaElement AND tab3.IRSpot = tab4.IRSpot 

TAB3返回一行{2,0}和TAB4返回多行,這些含有相同的值{2,0}八強。

我期待我的連接返回八行結果,因爲我正在執行連接但我只返回一個結果行?

+0

爲什麼你刪除了你的問題的主體!?我已回滾顯示您接受答案的問題。 – MatBailie

回答

2

將其更改爲右連接以找出不匹配的內容。 INNER JOIN將只顯示在兩個表中找到匹配的行。我懷疑這是最後的where/on子句,但是沒有看到表結構和數據,很難說清楚。

SELECT tab4.IRSpot as IRSpot, tab4.IRVol AS MaxPlauShift, tab4.VegElement AS MaxPlauLoss FROM 
    (SELECT tab1.IRSpot, MIN(tab1.VegaElement*Probability) AS PlausibleLoss FROM 
     (SELECT IRSpot, IRVol, VegaElement 
     FROM SEL_VegaElementMatrix()) tab1 
    JOIN 
     (SELECT IRSpot, IRVol, Probability 
     FROM Table_Name_2) tab2 
    ON tab1.IRSpot = tab2.IRSpot AND tab1.IRVol = tab2.IRVol 
    GROUP BY tab1.IRSpot) tab3 
RIGHT JOIN 
    (SELECT IRSpot, IRVol, VegaElement 
    FROM SEL_VegaElementMatrix()) tab4 
ON tab3.PlausibleLoss = tab4.VegaElement AND tab3.IRSpot = tab4.IRSpot