Oracle sub select query qith aggregation function -
id firstname lastname 1 john smith 2 mike smith 3 ben bray 4 john bray 5 john smith 6 bill lynch 7 bill smith
hi there. i'm trying subselect query in oracle min , max functions, can't seem on right track. appreciated.
example: i'm looking in table carl brayland. if doesn't exist want first name before carl brayland give me id 3 -> ben bray
example2: i'm looking in table mike mad if doesn't exist want first name before mike mad give me id 6 -> bill lynch
last name should leading.
use rownum on sorted set (here :p_lastname := 'mad'; :p_firstname := 'mike';
):
sql> my_data ( 2 select 1 id, 'john' firstname, 'smith' lastname dual 3 union select 2, 'mike', 'smith' dual 4 union select 3, 'ben' , 'bray' dual 5 union select 4, 'john', 'bray' dual 6 union select 5, 'john', 'smith' dual 7 union select 6, 'bill', 'lynch' dual 8 union select 7, 'bill', 'smith' dual 9 ) 10 select id, firstname, lastname 11 (select id, firstname, lastname 12 my_data 13 lastname <= :p_lastname 14 , (lastname < :p_lastname or firstname <= :p_firstname) 15 order lastname desc, firstname desc) 16 rownum = 1; id firstname lastname ---------- --------- -------- 6 bill lynch
Comments
Post a Comment