0

我有以下 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
4

1 回答 1

0

这可能是一个愚蠢的答案,如果是,请原谅。

行号是从零开始的吗?如果将 where 子句更改为:

WHERE rn = 0

?

编辑:我还注意到该行中可能缺少“AS”:

SELECT * , ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY Col5 ASC, Col3 ASC) rn

应该是:

SELECT * , ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY Col5 ASC, Col3 ASC) AS rn
于 2012-06-08T14:11:16.780 回答