I am facing a pecular problem in writing a sql query for below functionality:
Consider below table:
---------------------------------
AccountNumber JobNumber
---------------------------------
1234 1111113
1234 1111112
1234 1111111
1212 1111131
1212 1111132
I want to fetch a latest job number for account number passed to a query. For eg: If I pass 1234 as a account number, I need to get 1111113 and if I pass 1212 I should get 1111131. How to write a PL/SQL query to achieve this? We will pass multiple account number like AccountNumber IN ('1234','1212'). So for each account number I need to get latest job number.
Currently I have tried without using IN like this:
SELECT *
FROM (SELECT JobNumber
FROM TABLE1
WHERE AccountNumber = ?)
WHERE ROWNUM = 1
and in Java JDBC I am looping thorugh account number ArrayList and executing JDBC against Oracle db. But as you know it is not a feasible solution, if there are 4000 accounts performance hits.
Can you help in writing this SQL Query?
EDIT: Here latest means highest jobnumber, for eg: out of 1111113 and 1111112. 1111113 is the latest data
There are several ways:
OR
OR