简体   繁体   English

使用row_number返回查询的特定行

[英]using row_number to return specific rows of query

I am using SQL Server 2012 & MATLAB. 我正在使用SQL Server 2012和MATLAB。 I have a table of 5 columns (1 char, 1 datetime and 3 floats). 我有5列的表格(1个字符,1个日期时间和3个浮点数)。 I have a simple query shown below that returns the data from this table which contains over a million records - this however causes an out of memory error in MATLAB. 我有一个下面显示的简单查询,该查询从该表返回包含一百万条记录的数据-但是,这会导致MATLAB中出现内存不足错误。

simple query 简单查询

 select id_co, date_r, FAM_Score, FAM_A_Score, FAM_Score
 from GSI_Scores
 where id_co <> 'NULL'
 order by id_co, date_rating

So I was looking to breakdown the query select the data in batches of 250,000 records. 因此,我希望对查询进行细分以选择250,000条记录中的数据。 I have just come across the ROW_NUMBER function which I added to my query, please see below. 我刚刚遇到了我添加到查询中的ROW_NUMBER函数,请参见下文。 This numbers all the records for me. 这为我记录了所有记录。 However I am having trouble selecting say records between 250,000 and 500,000. 但是我在选择250,000到500,000之间的记录时遇到了麻烦。 How do I do this? 我该怎么做呢?

updated query 更新的查询

  select id_co, date_r, FAM_Score, FAM_A_Score, FAM_Score, row_number() over (order by id_co) as num_co
 from GSI_Scores
 where id_co <> 'NULL' and num_sedol between 250000 and 500000
 order by id_co, date_rating

Simply use a sub query or Common Table Expression (CTE). 只需使用子查询或通用表表达式 (CTE)。

;WITH CTE AS
(
  --Your query
)
SELECT * FROM CTE
WHERE num_co BETWEEN 250000 AND 500000

Just an sample example 只是一个示例

 declare @t table  (ID INT)
 insert into @t (id)values (1)
  insert into @t (id)values (2)
   insert into @t (id)values (3)
  insert into @t (id)values (4)

  ;WITH CTE AS
  (
  select *,COUNT(ID)OVER (PARTITION BY ID  ) RN from @t

 )
 Select ID from CTE C WHERE C.ID BETWEEN 2 AND 4 
 ORDER BY RN 

OR 要么

;WITH CTE (select id_co,
                        date_r, 
                        FAM_Score,
                        FAM_A_Score, 
                        FAM_Score, 
                        COUNT(id_co) over (PARTITION BY ID  DESC) as num_co
                from GSI_Scores
                    where id_co <> 'NULL')
            Select C.d_co,
            C.date_r, 
            C.FAM_Score,
            C.FAM_A_Score, 
            C.FAM_Score FROM CTE C
            WHERE C.id_co between 250000 and 500000
            order by C.id_co, C.date_rating

You could try using the OFFSET x ROWS FETCH NEXT y ROWS ONLY commands like this: 您可以尝试使用OFFSET x ROWS FETCH NEXT和ROWS ONLY这样的命令:

CREATE TABLE TempTable (
    TempID INT IDENTITY(1,1) NOT NULL,
    SomeDescription VARCHAR(255) NOT NULL,
    PRIMARY KEY(TempID))

INSERT INTO TempTable (SomeDescription) 
VALUES ('Description 1'),
    ('Description 2'),
    ('Description 3'),
    ('Description 4'),
    ('Description 5'),
    ('Description 6'),
    ('Description 7'),
    ('Description 8'),
    ('Description 9'),
    ('Description 10')

SELECT * FROM TempTable ORDER BY TempID OFFSET 3 ROWS FETCH NEXT 2 ROWS ONLY;

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

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