How to get values against oldest date field in Oracle

How to get values against oldest date field in Oracle



I have a select query which take values from multiple tables with date as one of the fields:


select e.id,e.date,d.d_id
from emp e, d_source d
where e.d_id = d.d_id and e.emp_id = 100`



For each emp_id, there are 2 or more records. I have to select id from emp table against the oldest date value. There are cases when the date field values are null or date field values are same for all the selected records. In such case I have to select the ID which is associated with a particular d_id (say 123) of d_source table.


emp_id


id


emp


d_id


d_source



What I have tried so far is


`select e.id from emp e
where e.emp_id = 100
and e.date=(select min(date) from emp where emp_id = 100)`



These are some results of the first select query for 3 different emp_id-



Select results



So, I need to return only 'id' of the record having oldest date value and if date field values are equal or empty, I need to return 'id' of the record which have d_id as 456



Any help would be appreciated.





You seem to have shown the same query twice, is that intentional or was the second one supposed to be trying to deal with the duplicate dates? Also, sample data and expected results might be useful. I'm also not clear how you want to deal with null dates - if there is a null and a not null which takes precedence? And where does the 123 value come from? (You're probably looking for something like this, though there are lots of other examples around; where you put your rules into the ranking function order by clause...)
– Alex Poole
Aug 27 at 18:02



order by





@Alex Poole, sorry my bad. Updated!
– Sandy
Aug 27 at 18:06





OK, so what is wrong with that query - is it that it doesn't match anything if the date field is null? Do you actually need the join to d_source - it doesn't seem to be doing anything? Again, sample data and results would be helpful.
– Alex Poole
Aug 27 at 18:32



d_source





All your examples have different ID; earlier you said that there were multiple rows per ID. And none of them are 100. This is a bit confusing. Is each pair of results you've shown supposed to have the same ID, so there are 3 different IDs overall?
– Alex Poole
Aug 27 at 18:42






@Alex Poole, appreciate your effort in looking into my problem. I have different 'id's for same 'emp_id'. I need to select only one 'id' against some condition with 'date'. Sorry for the confusion.
– Sandy
Aug 27 at 18:50




1 Answer
1



Here's an example, based on what I understood.


SQL> -- sample data
SQL> with emp (emp_id, id, cdate, d_id) as
2 (select 100, 11111, date '2010-02-26', 123 from dual union all
3 select 100, 22222, date '2018-02-26', 456 from dual union all
4 --
5 select 200, 11122, date '2010-02-26', 123 from dual union all
6 select 200, 22211, date '2010-02-26', 456 from dual union all
7 --
8 select 300, 11133, null, 123 from dual union all
9 select 300, 22244, null, 456 from dual
10 ),
11 source (d_id) as
12 (select 123 from dual union all
13 select 456 from dual
14 )
15 -- query that, hopefully, returns result you need
16 select emp_id, id, cdate, d_id
17 from (select e.emp_id, e.id, e.cdate, d.d_id,
18 row_number() over (partition by e.emp_id order by e.cdate, d.d_id desc) rn
19 from emp e join source d on e.d_id = d.d_id
20 )
21 where rn = 1
22 order by emp_id;

EMP_ID ID CDATE D_ID
---------- ---------- ---------- ----------
100 11111 26/02/2010 123
200 22211 26/02/2010 456
300 22244 456

SQL>





thank you for your effort. But this doesn't satisfy my request of selecting 'id' of the record which having d_id as 456. Please refer the image link included in the question. I need to return only 'id' of oldest date field and if date fields are equal or empty, I need to return 'id' of the record which have d_id as 456. TIA
– Sandy
Aug 27 at 20:24





I saw the image you posted, and I'd say that query I wrote returns result you need. Though, there are 3 separate cases on the image, while I put all of them into the same query. You'd just include a WHERE e.emp_id = some_value into my query.
– Littlefoot
Aug 27 at 20:53


WHERE e.emp_id = some_value





I did that. I'm looking for some option where I can mention the value of d_id when date values are same or null, not using order by clause for d_id. Probably a CASE WHEN clause?
– Sandy
Aug 27 at 21:09





I don't know; you should. You never explained which D_ID value to take, just that it has to be 456, which is the largest in the set.
– Littlefoot
Aug 27 at 21:10






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)