sql - oracle select from 2 table restrict from 1 table and order by another -


i have book , recipient table. want select maximum 20 rows order recipient table's membershipdate column. after got it, want order book table's id column. wrote sql. there way less code?

select  *    ( select    *                ( select    b.*                            book b                                 join recipient r on r.id = b.recipient_id                           b.bookno = 115                       order  r.membershipdate desc                     )               rownum <= 20         ) order id desc 

you can remove 1 layer of select:

select * (     select    b.*          book b     join recipient r on r.id = b.recipient_id         b.bookno = 115     order  r.membershipdate desc ) rownum <= 20 order id desc; 

selecting b.* isn't good, idea, it's better specify columns want, if want them - make sure them in order expect.

you can @ row_number() analytic function in place of rownum, give more code - not should matter, effectiveness , efficiency of query rather more important length.


Comments

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -