selecting highest numbered object Oracle 12c

Need to select the slip with the largest number when grouped by stock from objects below:

create table tab1(horse number, stock number);
insert into tab1 values(100,4);
insert into tab1 values(101,5);

create table tab2(stock number, slip number);
insert into tab2 values(4, 1);
insert into tab2 values(4, 5);
insert into tab2 values(4, 8);
insert into tab2 values(5, 1);
insert into tab2 values(5, 5);
insert into tab2 values(5, 6);
insert into tab2 values(5, 7);
commit;

Open in new window

desired results
horse  stock  slip
 100       4        8
 101       5        7
 
 select t1.horse, t2.stock, t2.slip,
   rank ()
                   over (partition by horse, slip
                         order by slip desc)
                       rnk
  from tab1 t1
  join tab2 t2
  on t1.stock = t2.stock
  where rnk = 1

Open in new window

returns error:  "rnk" invalid identifier....

all pointers & suggestions appreciated...
Jblue RAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sdstuberCommented:
SELECT horse, stock, slip
    FROM (SELECT t1.horse,
                 t2.stock,
                 t2.slip,
                 RANK() OVER(PARTITION BY horse ORDER BY slip DESC) rnk
            FROM tab1 t1 JOIN tab2 t2 ON t1.stock = t2.stock)
   WHERE rnk = 1
ORDER BY horse


Since rnk is defined in the select, you need to wrap the whole query in parentheses to make an in-line view so you can reference rnk outside.

Also note I changed your PARTITION BY clause to remove slip from the partition clause.  With slip included then every row of horse/slip is unique so every row will have rank 1.
Since you want the rows for each horse ordered by slip, then slip is only in the order by.


You could use ROW_NUMBER or DENSE_RANK instead of RANK here also.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jblue RAuthor Commented:
Perfect.  Thank you !
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.