How to query exact range of rows in Impala? - impala

I want to get a result set of rows from 1000 to 2000 from an ordered query.
In Oracle I would use condition "rownum >= 1000 and rownum <= 2000"
Is there some way to do same thing using Impala?

Managed to do it by using:
"ORDER BY field_id
LIMIT 1000 OFFSET 1000"

Related

Why LIMIT и OFFSET (OFFSET ... ROWS FETCH FIRST ... ROW only) functions in PostgreSQL are not working?

I am trying to use LIMIT and OFFSET functions or OFFSET ... ROWS
FETCH FIRST ... ROW only. PostgreSQL gives me the wrong number of rows in result.
select user_id, max(order_ts) as lastorder
from production.orders
group by user_id
order by lastorder desc, user_id desc
OFFSET 10 ROWS
FETCH FIRST 20 ROW only
or
select user_id, max(order_ts) as lastorder
from production.orders
group by user_id
order by lastorder desc, user_id desc
OFFSET 10
limit 20
Still gives me 20 rows (should be 10: from 10th row to 20th - is 10).
How is this? Any help, please?
LIMIT 20 tells server to return not more than 20 records. FETCH FIRST 20 ONLY is absolutely the same. The query might return 20 rows or less depending on the data and query conditions. If you are trying to get rows from 11th to 20th then you need to specify LIMIT 10 OFFSET 10.
See the paragraph LIMIT Clause in the documentation for details:
https://www.postgresql.org/docs/15/sql-select.html#SQL-LIMIT

RedisSearch get max date in JSON

I use RedisSearch to query my products
I want to get last 10 products changed
In SQL I can do that with:
Select top 10 * from products order by max(changedate) desc
How can I get the same result using RedisSearch?
You can use a range query with sortby.
FT.SEARCH products "#changedate:[somedate inf]" SORTBY changedate DESC
Please note that the smaller the date range you give, the faster the query will execute. You can fo [-inf inf] but that will not be efficiant.

How to get min and max with filter efficiently?

I have DB like this (it's actually a much bigger than this):
value
-------
10
20
50
600
70
800
I want to get min and max values, which satisfies some conditions (where).
I wrote these selects:
select value
from records
where value >= 50
order by value asc
limit 1
select value
from records
where value <= 600
order by value desc
limit 1
But can I do it more efficiently and in one select?
You can combine both conditions using and in WHERE clause and use min() and max() functions on value.
select max(value) as Maximum, min(value) as Minimum
from records
where value >= 50
and value <= 600
A more general approach uses conditional aggregation:
select max(value) filter (where value >= 50),
min(value) filter (where value <= 600)
from records ;
Filtering in the WHERE clause happens to work for the limits in your problem. However, it does not generalize to more than two values. And is cumbersome even for other pairs.

SQL - How to select a row having a column with max value without using sub query in Oracle

date value
18/5/2010 40
18/5/2010 20
20/5/2010 60
18/5/2010 30
17/5/2010 10
16/5/2010 40
18/5/2010 60
18/5/2010 25
Output
date value
18/5/2010 60
20/5/2010 60
I need to query for the row having max(value)(i.e. 60). So, here we get two rows.
the date can be in any order
Plz do not use SUBQUERY
I need a dynamic query without using sub query
NESTED QUERY will be fine ...
I have tried that using rownum ... Where rownum< some_value ...but it is not dynamic
In 12c the FETCH clause allows you to do this
select * from the_table
order by value desc
FETCH FIRST 1 ROWS WITH TIES;

Query rows 9,000,000 to 10,000,000

New to postgresql, I have a table with 10,000,000 rows, I've been querying data a million rows at a time
SELECT mmsi, report_timestamp, position_geom, ST_X(position_geom) AS Long,ST_Y(position_geom) AS Lat
FROM reports4
WHERE position_geom IS NOT NULL
ORDER by report_timestamp ASC
LIMIT 1000000
OFFSET 8000000
When I try and query the last million rows nothing shows up
SELECT mmsi, report_timestamp, position_geom, ST_X(position_geom) AS Long,ST_Y(position_geom) AS Lat
FROM reports4
WHERE position_geom IS NOT NULL
ORDER by report_timestamp ASC
LIMIT 1000000
OFFSET 9000000
Not sure If I'm doing the query right, or im overlooking something.
The table may have 10,000,000 rows, but how many of those rows have WHERE position_geom IS NOT NULL?
What do you get with:
SELECT count(*)
FROM reports4
WHERE position_geom IS NOT NULL;