简体   繁体   中英

How output distinct number of rows in SQL Server 2012

I have a query that results in more than 1 million of records in result table.

But I have to use Excel for further processing and it has a limit for such big data.

How can I query eg first 500.000 records and then the last?

I use SQL Server 2012.

Thanks in advance!

You could use the OFFSET FETCH clause like this:

/* Fetch the first 500k rows */
SELECT col1, col2, ... 
FROM TheTable 
ORDER BY Col1 OFFSET 0 ROWS FETCH NEXT 500000 ROWS ONLY;

/* Fetch the next 500k rows */
SELECT col1, col2, ... 
FROM TheTable 
ORDER BY Col1 OFFSET 500000 ROWS FETCH NEXT 500000 ROWS ONLY;

You will have to trim the values used for offset and fetch next to suit the number of rows you want to limit each query by and add extra fetch queries if you need to fetch more than the 1 million in my example.

You can use

select TOP 500000 * from table order by id  -- to get the first half..

The bottom half requires knowing how they are ordered.. Assume ID is an ordering field

select * from
 (select top 500000 * from table order by id DESC) xx
order by xx.id

SELECT TOP 500000 * FROM table_name

如果要获取表中最底端的值,请执行ORDER BY column_name ASCDESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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