我有以下SQL查詢。我刪除了內部工作,但顯示了結果的黑盒效應。來自SQL查詢的奇怪結果?
整個查詢不會返回{0,10,5,-2,10},因爲它應該,而是它返回{0,0,0,0,0},我不明白爲什麼分區由線正在影響它?
如果我排除WHERE rn = 1
條件,它可以正常工作。因此,出於某種原因,行號無法正確查看。
我認爲這個問題是特定於{0,10,5,-2,10}的值,無論什麼原因,因爲當我用不同的參數調用這個函數時,這些值會返回正常。
SELECT Col1, isnull(Col2,0) AS Col11, isnull(Col4,0) AS Col4, isnull(q.Col3,0) AS Col3, isnull(q.Col5,0) AS Col5
FROM
(
SELECT * , ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY Col5 ASC, Col3 ASC) rn
FROM
(SELECT 0 AS Col1, tab34.Col2, tab34.Col4, tab34.Col3, Col5 FROM
(SELECT PST FROM Func2('x','y')) tab3
LEFT JOIN
(SELECT Col2, Col4,Col3,abs(tab33.Value + Col6) AS Value, Col7 AS Col5 FROM
(SELECT tab11.Col2, tab11.Col4, tab11.Col3, Value FROM
(SELECT Col2, Col4, Col3 FROM Table2 WHERE Col8 = 'y' AND Col10 = 1) tab22
JOIN
(SELECT Col2, sum(Value) AS Value, Col4, Col3 FROM Table1 WHERE Col9 = 'x' AND Col8 = 'y' GROUP BY Col2, Col4, Col3) tab11
ON tab11.Col2 = tab22.Col2 AND tab11.Col4 = tab22.Col4 AND tab11.Col3 = tab22.Col3
WHERE Value < 0) tab33
JOIN
(SELECT Col11, Col6, Col7 FROM dbo.Func1('x','y')) Shifts
ON Shifts.Col11 = tab33.Col2) tab34
ON tab3.PST = tab34.Value
) VolData
) q
WHERE rn = 1
當你執行它沒有'其中RN會發生什麼= 1'? – Blorgbeard
我從來沒有嘗試過 - 我得到正確的結果?我總是選擇第一個結果,以防我的一個連接返回多行。爲什麼是這樣的,我該如何解決? – mezamorphic
您能否提供一些演示此問題的示例數據? –