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

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;

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

Max() in impala not resulting correct value

I have 3 rows with column range and i want to pull Max value using the query in impala. But the resulted output returning 8 instead of 30
range
-----
-15
8
30
Query:
select max(range) from table1
Output is resulting
8 instead 30.
i also tried retrieving the record in asc order interestingly it resulted in value in order like
-15
30
8
which is wrong.
The value is stored as a number instead of a string. You can cast before taking the max. Assuming they are integers:
select max(cast(range as int))
from table1 ;

How to query exact range of rows in 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"

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.

Oracle SQL : comparing sum of rows data to one column

I have following type of data, where sum of interval_mins is supposed to be equal to Total_mins for a particular ID#. I need the rows where it is missing a row for Interval_mins e.g. for ID# 20. How can I do that? Thanks,
ID Date Interval_mins Total_mins
10 4/1/16 60 80
10 4/1/16 20 80
20 4/1/16 60 85
If you are just looking for ids were the totals do not match:
select id
from t
group by id
having max(total_mins) <> sum(interval_mins);