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 e.g. 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
If you want to get the bottom-most values in the table, do an ORDER BY column_name ASC or DESC
Related
I am able to grab this first fifth but how do I get the other four fifths?
SELECT TOP 20 PERCENT
...
you can achieve pagination using CTE. you have to give the starting row number and the ending row number to get the required number of rows.
DECLARE #StartingRow int, #EndingRow int
SET #StartingRow = 10
SET #EndingRow = 20
;with CTE as(
SELECT *,
ROW_NUMBER() OVER( ORDER BY ID ) AS recordno
FROM Table
)
SELECT * FROM CTE WHERE recordno BETWEEN #StartingRow AND #EndingRow
In SQL Server you can use OFFSET and FETCH NEXT ROWS to select n number of rows after starting from nth row.
For example if ID is your order by column name and you have exactly 100 rows in the table then you can use below five queries. In first query OFFSET 0 Rows will indicate to select rows starting from first one. In second query select list will start from 21st row. FETCH NEXT 20 ROWS ONLY will ensure that after starting row 20 rows (if available) will be selected in each query
SELECT * FROM Table ORDER BY ID
OFFSET 0 ROWS
FETCH NEXT 20 ROWS ONLY
SELECT * FROM Table ORDER BY ID
OFFSET 20 ROWS
FETCH NEXT 20 ROWS ONLY
SELECT * FROM Table ORDER BY ID
OFFSET 40 ROWS
FETCH NEXT 20 ROWS ONLY
SELECT * FROM Table ORDER BY ID
OFFSET 60 ROWS
FETCH NEXT 20 ROWS ONLY
SELECT * FROM Table ORDER BY ID
OFFSET 80 ROWS
FETCH NEXT 20 ROWS ONLY
Consider the query below.
Select *
From table
Where name = 'stackoverflow'
Order By age
This is the query I am interested in. However I want to combine this with limit and offset as well. So this is what I did now.
Select
*,
ROW_NUMBER() OVER (ORDER BY primary_id DESC) as ROW_NUMBER
From
table
Where
name = 'stackoverflow'
Order By
age,
Offset 10 rows Fetch Next 20 Rows Only
The problem is that I am getting wrong results. I want to first query all rows based on where name = 'stackoverflow' and then order By age and then only fetch some rows based on limit and offset.
You have two order by clause perhaps you just need one :
select t.*
from table t
where name = 'stackoverflow'
order by age
offset 10 rows
fetch next 20 rows only;
I am working on python sqlite3.
This statement gets records 5 - 14;
SELECT * FROM something LIMIT 5, 10;
But how do I get, lets say the first five and last five records through a single statement?
You can combine output of two select statement like this:
(SELECT * FROM `something` order by some_column_name
limit 0,5)
union
(SELECT * FROM `something` order by some_column_name desc
limit 0,5
)
Specify some ordering of rows, so that it will select rows accordingly.
Maybe the better way is using rowid in your order by clause to get the first and last rows based on inserting the rows:
select test from
(select test,rowid from table1 order by rowid asc limit 0,5)t1
union all
select test from
(select test,rowid from table1 order by rowid desc limit 0,5)t2;
Here is a sample in SQL Fiddle
If I say:
select * from table order by col1 where rownum < 100
If the table has 10 million records, will Oracle bring all 10 million, sort it and then show me the first 10? Or is there a way it will optimise it?
If you do this
select * from table order by col1 where rownum < 100
then Oracle will throw an error as the WHERE clause comes before the ORDER BY.
If you do this
select * from table where rownum < 100 order by col1
then Oracle will return a random 99 records as the WHERE clause comes before the ORDER BY.
If you want to return a the first 100 records, ordered by a column, you must put the order by in a sub-select.
select *
from ( select * from table order by col1 )
where rownum <= 100
Oracle will do the sort, how else will it know the records you want? However, it will be a sort with a stopkey because of the ROWNUM. Oracle doesn't actually sort the entire result set, as some optimisation goes on under the hood, but this is what you can assume takes place.
Please see this article by Tom Kyte.
I am working with mysql. I need queries that fetch a list of maximum values.
I know how to get one maximum value i.e using max(colName) in select query.
Is there some similar query that can fetch more than 1 maximum values like the top 10 values.
select * from table order by colName desc limit 10;
SELECT * FROM table
ORDER by price DESC
LIMIT 10
try
select distinct colName from tablename order by colName desc limit 10;