How to select statment to data dividing to 3 parts of rows ?

ahmed salah 3,216 Reputation points
2020-08-19T00:31:22.713+00:00

I have table have 6000 rows to books data table without Id I need to display data dividing to 3 parts

from 1 to 2000

from 2001 to 4000

from 4001 to 6000

so how to do that please ?

create table #books
(
ISBN nvarchar(100),
Author nvarchar(100)
)
insert into #books(ISBN,Author)
VALUES
('002-80023334','Michel'),
('002-70023334','Hazem'),
('002-30023334','raheem'),
('002-46023334','salah'),
('002-56023334','mamdooh'),
('002-70123334','henry'),
('002-80223334','maxoweel')

so How to make select statement to select data from table Books

from 1 to 2000

from 2001 to 4000

from 4001 to 6000

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,597 questions
{count} votes

Accepted answer
  1. Yitzhak Khabinsky 25,726 Reputation points
    2020-08-19T16:40:45.56+00:00

    Hi Ahmed,

    Here is a conceptual example for SQL Server 2012 onwards.
    It is using SELECT with OFFSET / FETCH clauses.
    Exactly what you need.

    -- DDL and data population, start
    DECLARE @tbl TABLE (
       ID INT PRIMARY KEY
       , [Description] VARCHAR(100) NOT NULL
       );
    
    INSERT INTO @tbl
    VALUES (1, 'One')
       , (2, 'Two')
       , (3, 'Three')
       , (4, 'Four')
       , (5, 'Five')
       , (6, 'Six')
       , (7, 'Seven')
       , (8, 'Eight')
       , (9, 'Nine')
       , (10, 'Ten')
       , (11, 'Elleven')
       , (12, 'Twelve')
       , (13, 'Thirteen');
    -- DDL and data population, end
    
    DECLARE @BatchNo INT = 1
       , @NumberOfBatchesTotal DECIMAL = 3
       , @RowsPerBatch INT
       , @RowTotal INT;
    
    -- To calculate # of rows per each Batch
    SET @RowTotal = (SELECT COUNT(*) FROM @tbl);
    SET @RowsPerBatch = CEILING(@RowTotal/@NumberOfBatchesTotal);
    
    WHILE @BatchNo <= @NumberOfBatchesTotal
    BEGIN
     SELECT * FROM @tbl
     ORDER BY ID
     OFFSET (@BatchNo - 1) * @RowsPerBatch ROWS
     FETCH NEXT @RowsPerBatch ROWS ONLY
    
       SET @BatchNo += 1;
    END;
    
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. EchoLiu-MSFT 14,581 Reputation points
    2020-08-19T01:33:28.743+00:00

    Hi ahmedsalah,
    I am not sure what field your table is sorted by, so I chose the author to sort.I did a test according to the data you provided:

     select * from (select * ,row_number() over(order by Author) rn from #books) t where rn between 1 and 2  
     select * from (select * ,row_number() over(order by Author) rn from #books) t where rn between 3 and 5  
     select * from (select * ,row_number() over(order by Author) rn from #books) t where rn between 6 and 7  
    

    18573-image.png

    Your 6000 rows of data can be displayed separately according to the following code:

    select * from (select * ,row_number() over(order by Author) rn from #books) t where rn between 1 and 2000  
    select * from (select * ,row_number() over(order by Author) rn from #books) t where rn between 2001 and 4000  
    select * from (select * ,row_number() over(order by Author) rn from #books) t where rn between 4001 and 6000  
    

    If you have any question, please feel free to let me know.

    If the response is helpful, please click "Accept Answer" and upvote it.

    Regards,
    Echo

    0 comments No comments

  2. Jingyang Li 5,891 Reputation points
    2020-08-19T02:53:59.173+00:00

    You can check out NTILE function to see whether that works for your case.

    ;with mycte as (
    select *, NTILE(3) OVER ( ORDER BY ISBN) grp
    from #books
    )
    Select * from mycte
    Where
    grp=1
    --or grp=2
    --or grp=3

    0 comments No comments

  3. Guoxiong 8,201 Reputation points
    2020-08-19T15:08:46.59+00:00

    You can load data to the table variable with an IDENTITY column and then you can select the rows based on the IDENTITY column values:

    DECLARE @Books TABLE (
     RowId int IDENTITY(1, 1),
     ISBN nvarchar(100),
     Author nvarchar(100)
    );
    
    INSERT INTO @Books (ISBN, Author)
    SELECT ISBN, Author 
    FROM #books 
    --ORDER BY ISBN; -- Sorted by ISBN 
    --ORDER BY Author; -- Sorted by Author 
    -- Random
    
    -- List first 2000 rows
    SELECT ISBN, Author FROM @Books WHERE RowId <= 2000;
    -- List 2000 rows from 2001 to 4000
    SELECT ISBN, Author FROM @Books WHERE RowId > 2000 AND RowId <= 4000;
    -- List 2000 rows from 4001 to 6000
    SELECT ISBN, Author FROM @Books WHERE RowId > 4000 AND RowId <= 6000;
    
    0 comments No comments