如何从 SQL Server 中选择 y 行之后的数据顶部 x 数据
例如,我有一个包含 10'000 行的表。我想选择前 500 行之后的前 100 行。我怎样才能最有效地做到这一点。
SQL Server 2008 所需的查询
例如我已经有这个查询,但我想知道是否有更有效的解决方案
SELECT TOP 100 xx
FROM nn
WHERE cc NOT IN
(SELECT TOP 500 cc
FROM nn ORDER BY cc ASC)
For example I have a table which contains 10'000 rows. I want to select top 100 rows after top 500th row. How can I do this most efficiently.
Query needed for SQL Server 2008
For example i have this query already but i wonder are there any more effective solution
SELECT TOP 100 xx
FROM nn
WHERE cc NOT IN
(SELECT TOP 500 cc
FROM nn ORDER BY cc ASC)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
教程 25:高效分页大量数据
Tutorial 25: Efficiently Paging Through Large Amounts of Data
选择 TOP 500,然后将 TOP 100 连接到结果集。
通常,为了值得这样做,您需要有一些标准来确定您需要 500 条记录,并且只需要 100 条记录对于另一个条件。我假设这些条件是针对您想要的 TOP 500 的 条件 1,以及针对您想要的 TOP 100 的条件2。由于条件不同,这就是 TOP 100 的记录可能不相同的原因。
EDIT #1
在您发表评论后,我更好地理解了您想要实现的目标。试试这个:
这有帮助吗?
Selecting TOP 500, then concatenating the TOP 100 to the result set.
Normally, in order to worth doing this, you need to have some criteria on which to base what your need 500 records for, and only 100 for another condition. I assume that these conditions are condition1 for the TOP 500, and condition2 for the TOP 100 you want. Because the conditions differ, that is the reason why the records might not be the same based on TOP 100.
EDIT #1
After your comment, I better understood what you want to achieve. Try this:
Does this help?