如何避免嵌套此查询。我认为这会使查询膨胀并使其难以阅读。我知道我可以将Row_number() OVER (ORDER BY id ASC)
放在where
条款中,但有没有办法实现呢?
SELECT *
FROM (SELECT [id],
[title],
[image],
[description],
[column5154],
[column5155],
[column5156],
RowNumber1 = Row_number() OVER (ORDER BY id ASC)
FROM [TestTable] x
WHERE x.rownumber1 % 5 = 0
答案 0 :(得分:3)
如果你想要每隔五行,不关心RowNumber1
,并且不想使用子查询,你可以这样做:
select top (20) percent t.*
from testtable t
order by row_number() over (order by id asc)) % 5;
order by
允许使用窗口函数。就个人而言,我认为子查询更容易理解。