Search code examples
oracle-databaseoracle10gtop-n

Need help in writing SQL query


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


Solution

  • There are several ways:

    SQL> create table t1(AccountNumber, JobNumber) as
      2  (
      3  select 1234,  1111113 from dual union all
      4  select 1234,  1111112 from dual union all
      5  select 1234,  1111111 from dual union all
      6  select 1212,  1111131 from dual union all
      7  select 1212,  1111132 from dual
      8  )
      9  ;
    
    Table created
    
    
    
    
    SQL> select t.AccountNumber
      2       , max(t.JobNumber) as JobNumber
      3    from t1 t
      4   where AccountNumber in (1212, 1234)  -- for example
      5   group by AccountNumber
      6  ;
    
    ACCOUNTNUMBER  JOBNUMBER
    ------------- ----------
             1234    1111113
             1212    1111132
    

    OR

    SQL> select AccountNumber
      2       , JobNumber
      3    from ( select t.AccountNumber
      4                , t.JobNumber
      5                , row_number() over(partition by t.AccountNumber order by t.JobNumber desc) rn
      6             from t1 t
      7            where AccountNumber in (1212, 1234) -- for example
      8         ) t
      9  where t.rn = 1
      10  ;
    
    ACCOUNTNUMBER  JOBNUMBER
    ------------- ----------
             1212    1111132
             1234    1111113
    

    OR

    SQL> select AccountNumber
      2       , JobNumber
      3    from ( select t.AccountNumber
      4                , t.JobNumber
      5                , max(JobNumber) over(partition by t.AccountNumber) mjn
      6             from t1 t
      7            where AccountNumber in (1212, 1234)   -- for example
      8         ) t
      9  where t.JobNumber = t.mjn
      10  ;
    
    ACCOUNTNUMBER  JOBNUMBER
    ------------- ----------
             1212    1111132
             1234    1111113
    
    SQL>