SQL query, offset and limit - sql

I'm using splunk. Postgres SQL.
I have 2.1M rows to pull up in ASC order.
This is my standard query that is working:
WHERE we_student_id = 5678
ORDER BY audit.b_created_date DESC NULLS LAST
then I usually use: (if data is more than 1-2M. I split them into batches)
FETCH FIRST 500000 ROWS ONLY
OFFSET 500000 ROWS FETCH NEXT 500000 ROWS ONLY
this time, my client requested to extract them by ASC order based on audited id not audit_created_date.
I used:
WHERE student_id = 5678
ORDER BY audit.audited_id ASC NULLS LAST
==========
I tried to pull up the first 500k.
I used:
*ORDER BY ASC NULLS LAST
LIMIT 500000 OFFSET 0*
The result is just 100k.
I tried to put maxrows=0 before my select statement with the same query
*ORDER BY ASC NULLS LAST
LIMIT 500000 OFFSET 0*
but I'm getting an error: canceling statement due to user request.
I tried this query to get the first 400k instead of 500k and removed the OFFSET 0. And I'm still using maxrows=0 before my select statement
*ORDER BY ASC NULLS LAST
LIMIT 400000*
There's a result 400k.
When I tried to extract the next 400k, I queried
*LIMIT 400000 OFFSET 400000*
I encountered the error again: canceling statement due to user request.
Usually, I can pull up 2M rows on Database. I usually use "FETCH FIRST 1000000" Then offset the other batch. My usual query on DB is
ORDER BY DESC NULLS LAST and use FETCH first and OFFSET
But this time, my client wants to get the data by ASC order.
I tried FETCH FIRST 400000 ROWS ONLY query and there's a 400k result. but whenever I increase the number to 500000, I get this error: canceling statement due to user request.
I usually use maxrows=0 because Splunk only shows the first 100k rows. Most of my data are 1-2 Million.
This error only happened when the client requested the reports by ASC order.
I just want to pull up the 2.1M rows on the database and I don't know how to pull it up by ASC order. I don't know if I'm using OFFSET and LIMIT correctly.

Related

Returning Only Latest Result For Location in SQL [duplicate]

This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
Closed last year.
I'm wanting to find the very last pallet placed in a given batch of locations within a warehouse.
I currently have:
SELECT
max(datreg) AS "_Reg Date",
logguser,
mha,
rack,
horcoor,
vercoor
FROM
L16T3
WHERE
l16lcode = '3'
AND
rack = #('Rack?',rack)
AND
horcoor >= #('Loc From?',horcoor)
AND
horcoor <= #('Loc To?',horcoor)
ORDER BY 1
LIMIT 1
I thought this would return just the last pallet placed in that specific location, but I'm still getting like 4 entries for one location.
I would only want the highlighted result, as that is the most recent pallet placed in 110-001-04:
I'm sure this is super simple but im just starting out :)
You can use a combination of ORDER BY and LIMIT to achieve what you want.
Limit
In a lot of other databases, this is called LIMIT, but I missed that you are using an Oracle database, which has a different dialect of SQL. In Oracle, the most direct equivilent of a limit is:
FETCH FIRST n ROWS ONLY
This means that your query can return at most n rows. So, for example, FETCH FIRST 1 ROWS ONLY means that it can return at most 1 row. The issue is that it takes rows from the start of the table, not the end (and despite the wording implying FETCH LAST n ROWS ONLY would be a thing, it doesn't seem to be) --- you can essentially think of it as cutting off the rows below given limit.
For example, if I have rows in order "A", "B", and "C", FETCH FIRST 1 ROWS ONLY only returns "A". If "C" was really the one I wanted (e.g. the row at the bottom), then I would need to add an ORDER BY clause to first order the results so that the one I want is at the top.
Order By
ORDER BY column dir orders your results by a specific column in a specific direction, e.g. in ascending (ASC) or descending (DESC) order. The syntax actually allows for more complex ordering (e.g. ordering by multiple columns or by a function), but for this simple case this should do what we need.
Putting it together
You want to order so that your desired row is at the top of your table, then you want to limit your results set to contain at most one row.
Adding something like this to the end of your query should work:
ORDER BY "_Reg Date" DESC
FETCH FIRST 1 ROWS ONLY

Getting random rows in Big Query with different limit?

I have the following queries :
SELECT * FROM `datafusiontest-2897325.mergedquery.test_table LIMIT 10
SELECT * FROM `datafusiontest-2897325.mergedquery.test_table LIMIT 100
SELECT * FROM `datafusiontest-2897325.mergedquery.test_table LIMIT 10000
I am getting a different top result for each query.
As your query is not specifying an order, it is normal for results to be different each time - they are returning random rows from your table which meet the qualifying criteria.
To get the same top n returned you should add an ORDER BY clause, for example:
SELECT *
FROM `datafusiontest-2897325.mergedquery.test_table`
ORDER BY date
LIMIT 10

Resource Exceeded error message in order by

I have a destination table(created as an output of some other query),
simple order by on one of its column is resulting "resource exceeded" error message.
Destination table created has 8.5 million rows and 6 columns (size 567 MB approx).
select col1,col2.....col6 from desttable order by col 5 desc
is resulting "resource exceeded" error message.
Remove ORDER BY and see if error disappear!
ORDER BY moves WHOLE data into one worker - thus resources exceeded
If I am adding "LIMIT" and "OFFSET" clause in the query after order by
its working,even though LIMIT clause is the last to be evaluated.How
it is working there??
When you add LIMIT N - query runs on multiple workers. Each worker gets only part of the data to process and outputs only respective N rows. Those N rows from all workers than gets "delivered" to one worker where final ORDER BY and LIMIT occurs and "winning" N rows becomes output of whole query

How to use LIMIT in query in access, but not TOP

I would like to display 15 rows from queries but not first 15?
SELECT Abgänge.Vorgang, Abgänge.Date_SW_Drucken FROM Abgänge
WHERE Abgänge.Bezahlung = "Bar" LIMIT 34,15;
How to transform this to access 2010?
Limit:
LIMIT from_record-1, count_record
You can't, because there is no support for an offset in the Microsoft Access SELECT syntax. An option is to use TOP with offset + limit and skip offset rows manually. BTW: Using TOP or LIMIT without an ORDER BY is not advisable as it can lead to inconsistent results.
You could also combine two queries with TOP, first selecting limit + offset, and then selecting only offset, for example
SELECT TOP 15 ...
FROM (
SELECT TOP 49 ....
FROM sometable
ORDER BY somecolumn ASC
) a
ORDER BY somecolumn DESC
The only problem with this solution is that if there are less than 49 results from the subquery, then the offset will be less than 34.
If you need the result in a different order then you may need to add an additional 'layer' that applies that order.

Total row count with OFFSET .. FETCH issue

Goal is trivial: get total row count and some data page.
When I use OFFSET...FETCH approach to implement paging with total row counting I running into following issue: when we pass some big page number (e.g. we have only 100 rows, but requested 15th with 10 records per page) COUNT(*) OVER() statement has never called, because result set is empty. So, we can not get right total row count in this case.
Is there way to get right total row count using OFFSET ... FETCH approach even when big page number passed?
FYI, OFFSET ... FETCH approach is that:
SELECT
...
Total = COUNT(*) OVER()
FROM Table1
ORDER BY Col1
OFFSET (#PageNum-1) * #PageSize ROWS
FETCH NEXT #PageSize ROWS ONLY;
I think the answer is "no". You are appending the total row count onto each row being returned. The query is returning no rows, so there is no place to put the total.
By the way, I do imagine that the total is being calculated. But without any rows, you never see it.
EDIT:
The only work-around I can think of is at the application layer. If no rows are returned, then run:
SELECT Total = COUNT(*) OVER()
FROM Table1;
You could actually run this first to get the total. The downside is when the table really isn't a table but a view that is expensive to run.