Reputation: 18123
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
Upvotes: 0
Views: 162
Reputation: 27251
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>
Upvotes: 1
Reputation: 425198
Won't a simple max() work?
SELECT max(JobNumber)
FROM TABLE1
WHERE AccountNumber = ?
And for multiple accounts in one query:
SELECT AccountNumber, max(JobNumber)
FROM TABLE1
WHERE AccountNumber IN (?, ?, ?)
GROUP BY AccountNumber;
Upvotes: 1
Reputation: 1161
select a, j1
from(
select
id,
a,
j,
first_value(j) over(partition by a order by id) j1
from (
select AccountNumber a, JobNumber j, row_number() over(order by 0) id
from table1) t
) t1
group by a, j1
Upvotes: 0
Reputation: 109597
I would start with standard SQL first. I hope the following does it.
SELECT JobNumber
FROM TABLE1
WHERE id = (SELECT MIN(id)
FROM TABLE1
WHERE AccountNumber = ?)
Upvotes: 0