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