Quantcast
Channel: Planet PostgreSQL
Viewing all articles
Browse latest Browse all 9976

Chris Spotts: Why Oracle rownum just isn't LIMIT...*sigh*

$
0
0
There times (not often, but times) where its really useful to write something like...

In Postgres I can do this:

SELECT
...
(SELECT some_value from table_bar bar where bar.foo_id = foo.foo_id
order by some_date LIMIT 1) as some_value_from_bar
FROM
table_foo foo;


However in Oracle, there's rownum, but this doesn't work:
SELECT
...
(SELECT some_value from table_bar bar where bar.foo_id = foo.foo_id
and rownum = 1 order by some_date) as some_value_from_bar
FROM
table_foo foo;


That doesn't work because the where clauses there is evaluated before the order by.

Can't do this either:
SELECT
...
(SELECT some_value from ( SELECT some_value from table_bar bar
where bar.foo_id = foo.foo_id order by some_date) t1 where rownum = 1 ) as some_value_from_bar
FROM
table_foo foo;

The inner query doesn't have the scope to pull in from table_foo anymore.

You can get around this with JOINing and filtering, but often times that subselect performance is just fine and much much more clear.
I just wish there was a real LIMIT in Oracle (or that we did our main app in Postgres).

Viewing all articles
Browse latest Browse all 9976

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>