我有2個查詢:一個是用ANSI SQL編寫的,另一個是用oracle方言編寫的。 我認爲他們都必須給出相同的結果集,但這不是事實。首先查詢提供了385行,第二 - 只有25兩個相同的查詢給出不同的結果
第一:
SELECT idclient, cl.surname, sum(sub1.s)
FROM client cl JOIN incomestatement incst USING(idclient)
JOIN (SELECT c.idincome ID, sum(inst.total) AS s
FROM instalment inst JOIN credit c USING(idcredit)
WHERE inst.paydate > c.paydate AND c.isloaned = 1
GROUP BY c.idincome) sub1 ON incst.idincome = sub1.ID
GROUP BY idclient, cl.surname;
二:
SELECT c.idclient, c.surname, sum(sub.s)
FROM client c, incomestatement inc,
(SELECT sum(inst.total) as s, cr.idincome as id
FROM instalment inst, credit cr
WHERE inst.paydate > cr.paydate AND cr.isloaned = 1 AND cr.idcredit = inst.idcredit
GROUP BY cr.idincome
) sub
WHERE c.idclient = inc.idclient AND inc.income = sub.ID
group by c.idclient, c.surname;
那麼,爲什麼他們不給同樣的結果?
是的,兩個子查詢產生相同的數據集並且Client和IncomeStatement加入(例如:使用(idclient)從客戶端加入incomestatement中選擇idclient(idclient))生成相同的數據集。這些查詢甚至具有相同的計劃。但我不知道他們爲什麼給不同的數據集 – maks 2011-05-14 14:53:30
謝謝,我已經解決了這個問題。我在第二個查詢inc.income = sub.ID中誤認了。相反,我必須編寫inc.idincome = sub.ID,因爲idincome是主鍵 – maks 2011-05-14 15:07:10
這很奇怪......唯一的其他分解步驟是編寫沒有GROUP BY和SUM的最外層查詢(您只需選擇' sub.s'或'sub1.s'的值。這些也應該是相同的,因爲你到目前爲止所說的話,但這是我能看到的唯一的其他東西。**哦,你解決了它.. 。好! – 2011-05-14 15:09:05