简体   繁体   English

在SQL Server查询中动态选择记录范围

[英]Selecting record range dynamically in a SQL Server query

I have a big query to make and then I convert to JSON so I get OutofMemory Exception in JAVA during my conversion. 我要进行很大的查询,然后转换为JSON,因此在转换期间会在JAVA中收到OutofMemory异常。 What I'm trying to achieve is split the query dynamically calling it multiple time. 我要实现的目标是动态地多次拆分查询。

ie

SELECT ROWS x/10 multiply by (SELECT  COUNT(*) FROM myquery) to 
            y/10 multiply by (SELECT  COUNT(*) FROM myquery) 
FROM myquery

I'm calling this query from a python script on a http endpoint so I can separate my load as to call it ten times for example replacing x , y by 1to10. 我正在从http端点上的python脚本调用此查询,因此我可以将其负载分开调用十次,例如将x,y替换为1to10。

Example Select the first 0/10 to 1/10 records and then 1/10 to 2/10 and then 2/10 to 3/10 ... 9/10 to 10/10. 示例选择第一个0/10至1/10记录,然后选择1/10至2/10,然后选择2/10至3/10 ... 9/10至10/10。

You need a field to order your rows call it order_field . 您需要一个字段来对行进行排序,将其order_field And use the analytic function NTILE(10) to create 10 groups 并使用解析函数NTILE(10)创建10个组

Then pass a variable @YourPage to indicate what page you want 然后传递一个变量@YourPage来指示您想要的页面

  SELECT *
  FROM (
        SELECT *,
               NTILE(10) OVER (ORDER BY order_field) as page_number
        FROM yourTable
       ) T
  WHERE T.page_number = @YourPage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM