Oracle sql rownum between how to shrink the query [duplicate] - sql

This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
How ROWNUM works in pagination query?
(3 answers)
Oracle SELECT TOP 10 records [duplicate]
(6 answers)
Oracle "LIMIT n,m" equivalent [duplicate]
(1 answer)
Closed 2 years ago.
I would like to get records from 25 to 50. I write this code, but, it looks terrible with double select clause.
Select * From (
Select eto.*, rownum rn from employee_trip_orders eto
) where rn between 25 and 50 ;
How can i shrink it to use one select like that?
Select eto.*, eto.rownum rn from employee_trip_orders eto
where rn between 25 and 50 ;
I don't need the second one. Thanks. I have old 11c Oracle version and offset keyword is not suitting me

How can i shrink it to use one select like that?
Since you are on Oracle 11g you cannot. You must use subquery inline to achieve your desired output.
Select eto.*, eto.rownum rn from employee_trip_orders eto
where rn between 25 and 50 ;
That query will never return a row. ROWNUM value is incremented only after it is assigned. Please see How ROWNUM works in pagination query.
From Oracle 12c onwards, you could use the new Top-n Row limiting feature.

You are not using order by clause so what is the meaning of the rownum? In the end, You are only fetching random 26 (25-50 inclusive) records.
So You can achieve the desired result using the following code:
Select eto.*, rownum rn
from employee_trip_orders eto
where rownum<= 26 ;
Cheers!!

Related

query with top and order by - convert from MsSql to Oracle [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 2 years ago.
I need to convert this query from MsSql syntax to Oracle syntax:
select top 1 (convert(varchar, UPDATED_DATE, 23)) as date from DA_CATEGORY order by date desc
How do I do this?
I need the data from both DB types to be the same string / value.
You can use the fetch clause as follows:
select to_char(UPDATED_DATE,'YYYY-MM-DD') as date
from DA_CATEGORY
order by UPDATED_DATE desc
fetch first row only

How to find the largest range between related dates in a column? [duplicate]

This question already has answers here:
Oracle SELECT TOP 10 records [duplicate]
(6 answers)
Closed 3 years ago.
I have a database where there are bands in one column and another column where there are release dates of their albums, how do i find the oldest band based off of the difference between their release dates? I.e. the oldest band is not Band1 because they first released an album in 1968, with my method Band1 is only 7 years old (or active for 7 years could be better), while Band2 is 26 years old (years active) because 2001 - 1975 = 26.
For instance:
Band......Rel_date
Band1....12/12/68
Band1....08/05/75
Band2....09/02/75
Band2....05/05/99
Band2....03/05/01
Oracle 12c+
select band from your_table order by rel_date fetch first 1 rows only;
If you want to include ties, change it to with ties instead of only
EDIT
select band from t
group by band order by max(rel_date) - min(rel_date) desc fetch first 1 rows only;
EDIT2
For Oracle versions before 12c:
select * from
(
select band from t
group by band order by max(rel_date) - min(rel_date) desc
) where rownum = 1
If I understand what you're asking
SELECT BAND
FROM YOUR_TABLE
WHERE REL_DATE = (SELECT MIN(REL_DATE)
FROM YOUR_TABLE)
oughta do it.
EDIT
Based on further information
WITH cteBand_activity AS
(SELECT BAND,
MIN(RELEASE_DATE) AS FIRST_RELEASE_DATE,
MAX(RELEASE_DATE) AS LATEST_RELEASE_DATE,
TRUNC((MAX(RELEASE_DATE) - MIN(RELEASE_DATE)) / 365) + 1 AS YEARS_ACTIVE
FROM ALBUMS
GROUP BY BAND
ORDER BY YEARS_ACTIVE DESC)
SELECT BAND, YEARS_ACTIVE
FROM cteBand_activity
WHERE ROWNUM = 1
should give the result you're looking for.
dbfiddle here
Best of luck.

Collecting values from the upper rows [duplicate]

This question already has answers here:
Calculate a Running Total in SQL Server
(15 answers)
Closed 8 years ago.
I have a table in sql server and I want to summarize a cell with upper rows in T-Sql like this:
Quantity Total
-----------------------------
1 1
5 6
12 18
20 38
I use SQL server 2008, How can I achieve this?
Regards, Majid.
You are looking for a cumulative sum, it would appear. If you are using SQL Server 2012 or later, just do:
select quantity, sum(quantity) over (order by quantity)
from table t;
Note the order by quantity. You need to specify the ordering for the cumulative sum, typically doing another sum.
In earlier versions, you can do this using a correlated subquery:
select quantity,
(select sum(t2.quantity)
from table t2
where t2.quantity <= t.quantity
) as total
from table t;

SQL, Select Max 5 From Field

I'm using Visual Basic 2010 for a project of mine. It's a database project and I'm trying to do this:
I want to get 5 rows based on the 5 largest values in the field "percentage". Is It possible to do that with an SQL command?
Yes.
Select the percentage column, and order by it (desc). Then limit by 5.
SELECT percentage FROM x ORDER BY percentage DESC LIMIT 5
For SQL Server
SELECT TOP 5 percentage_col1 FROM Table1
ORDER By percentage_col1 DESC;

SQLite - SELECT TOP syntax error [duplicate]

This question already has answers here:
How to get Top 5 records in SqLite?
(8 answers)
Closed 6 years ago.
I'm trying to use the statement SELECT TOP 1 * FROM tasks WHERE dueDate < ?1 ORDER BY dueDate DESC but SQLite says near "1": syntax error. What's wrong?
Use LIMIT 1 at the end of the query instead of TOP 1 (which isn't valid sqlite syntax).
You might also need to remove the ? in dueDate < ?1, but I don't know sqlite well enough to be sure.