此查询基于 2 个表,其中某些行PerfData1
有多个子记录。行不是重复的,但有些id
是重复的。我只需要获取其中的 1 行,这也是一个分页查询,它需要获取总数或现有项目和最终子集。
查询工作正常。但是,我不确定内联视图(内部ORDER BY
)中设置的顺序是否会保留在最终输出中。到目前为止,是没有看到任何偏差。但我记得在 SQL Server 世界中这并不能保证,因此我需要 2 个ORDER BY
子句。
SELECT
All_Results_Table.*
FROM
(
SELECT
Rownum_Table.*,
COUNT(*) OVER() as sys___countAllFoundRecords,
ROWNUM sys___outputRownum
FROM
(
SELECT
ROW_NUMBER() OVER(PARTITION BY PerfData1.id ORDER BY PerfData1.id) AS sys___multivalueRowSequence, -- This is OPTIONAL when we DON'T want dupe IDs
ROW_NUMBER() OVER(order by 1) AS sys___innerRowNumber,
PerfData1.id,
PerfData1.c1 item,
PerfData2.c1 subitem
FROM
PerfData1 inner join
PerfData2 on PerfData1.id = PerfData2.fkId
WHERE (1 = 1)
ORDER BY id, subitem desc --<<-- WILL THIS ORDER STAY in the FINAL OUTPUT
) Rownum_Table
WHERE sys___multivalueRowSequence = 1 -- This is OPTIONAL when we DON'T want dupe IDs
) All_Results_Table
WHERE
sys___outputRownum BETWEEN 200 AND 230
ORDER BY
上述SQL中设置的顺序会保留在最终输出中吗?