How do I do the following?
select top 1 Fname from MyTbl
In Oracle 11g?
If you want just a first selected row, you can:
select fname from MyTbl where rownum = 1
You can also use analytic functions to order and take the top x:
select max(fname) over (rank() order by some_factor) from MyTbl
select max(fname) keep (dense_rank first order by some_factor ) from MyTbl
. –
Ironsmith SELECT *
FROM (SELECT * FROM MyTbl ORDER BY Fname )
WHERE ROWNUM = 1;
top X
one can change it to WHERE ROWNUM <= X
–
Badderlocks With Oracle 12c (June 2013), you are able to use it like the following.
SELECT * FROM MYTABLE
--ORDER BY COLUMNNAME -OPTIONAL
OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY
OFFSET 0 ROWS
apparently is not necessary, you can use FETCH NEXT 1 ROWS ONLY
or even FETCH FIRST ROW ONLY
, the order by is important or it will be equivalent to just using a WHERE rownum = 1
. I've even tried it in an OUTER APPLY instruction and it worked like Ms-SQL's TOP function there. –
Salify TIES
. Refer this for the cases when ties occur for version 12c +
and 12c -
–
Ironsmith You could use ROW_NUMBER()
with a ORDER BY
clause in sub-query and use this column in replacement of TOP N
. This can be explained step-by-step.
See the below table which have two columns NAME
and DT_CREATED
.
If you need to take only the first two dates irrespective of NAME
, you could use the below query. The logic has been written inside query
-- The number of records can be specified in WHERE clause
SELECT RNO,NAME,DT_CREATED
FROM
(
-- Generates numbers in a column in sequence in the order of date
SELECT ROW_NUMBER() OVER (ORDER BY DT_CREATED) AS RNO,
NAME,DT_CREATED
FROM DEMOTOP
)TAB
WHERE RNO<3;
RESULT
In some situations, we need to select TOP N
results respective to each NAME
. In such case we can use PARTITION BY
with an ORDER BY
clause in sub-query. Refer the below query.
-- The number of records can be specified in WHERE clause
SELECT RNO,NAME,DT_CREATED
FROM
(
--Generates numbers in a column in sequence in the order of date for each NAME
SELECT ROW_NUMBER() OVER (PARTITION BY NAME ORDER BY DT_CREATED) AS RNO,
NAME,DT_CREATED
FROM DEMOTOP
)TAB
WHERE RNO<3;
RESULT
with (select ... ) as
clause) does not change anything to this problem, CTE just aims in reading and supporting queries. Right? @Sarath Avanavu –
Sudan select * from (
select FName from MyTbl
)
where rownum <= 1;
You can do something like
SELECT *
FROM (SELECT Fname FROM MyTbl ORDER BY Fname )
WHERE rownum = 1;
You could also use the analytic functions RANK and/or DENSE_RANK, but ROWNUM is probably the easiest.
Use:
SELECT x.*
FROM (SELECT fname
FROM MyTbl) x
WHERE ROWNUM = 1
If using Oracle9i+, you could look at using analytic functions like ROW_NUMBER() but they won't perform as well as ROWNUM.
TOP
wasn't accepted as the ANSI means of limited the resultset outside of filtration. –
Impuissant SQL
but not in PL/SQL
in 8i
. You had to use EXECUTE IMMEDIATE
to use them in a PL/SQL
block. –
Gewgaw 8
and 8i
were different versions, and only the latter supported the analytics (since R2
, AFAIR). SQL
and PL/SQL
had different parsers, the procedural one did not understand analytics and some other things. EXECUTE IMMEDIATE
was parsed with SQL
parser. They were merged in 9i
. –
Gewgaw 8i R2
was released in 2000, we had a meeting on which I insisted on upgrading all our clients using previous versions (over 200
installations by that time) and stop supporting 8
. Developers in that company still consider it the most important solution, since the next end of support happened only in 2009
. –
Gewgaw I had the same issue, and I can fix this with this solution:
select a.*, rownum
from (select Fname from MyTbl order by Fname DESC) a
where
rownum = 1
You can order your result before to have the first value on top.
Good luck
To select the first row from a table and to select one row from a table are two different tasks and need a different query. There are many possible ways to do so. Four of them are:
First
select max(Fname) from MyTbl;
Second
select min(Fname) from MyTbl;
Third
select Fname from MyTbl where rownum = 1;
Fourth
select max(Fname) from MyTbl where rowid=(select max(rowid) from MyTbl)
© 2022 - 2024 — McMap. All rights reserved.