I'm using Oracle 10g.
My research at
SQL - How to select a row having a column with max value
and
http://jan.kneschke.de/projects/mysql/groupwise-max/
address what to do with max(row) and then finding other max(rows) But, I'm not there yet.
Criteria: Code for ID's max(enroll_date) must be 'WHEAT'.
I don't want the maximum enroll date where the code is 'WHEAT'. I want the row only if the code is 'WHEAT' for the maximum enroll date.
Question: How can I write query to meet criteria?
This doesn't work because it returns ID=30, Code = WHEAT where Date 12/25/2001 which is not the max(date) for that ID.
select ID, code, max(enroll_date)
from enrollment
where CODE = 'WHEAT'
group by ID, code
This doesn't work either because it too returns ID=30, Code = WHEAT, where date = 12/25/2001.
select ID, code, max(enroll_date)
from enrollment
group by ID, code
having code='WHEAT'
Here's my TABLE
ENROLLMENT
===========================
ID CODE ENROLL_DATE
--------------------------
01 WHEAT <NULL>
01 WHEAT 12/21/2007
01 WHEAT 7/30/2009
30 WHEAT 12/25/2001
30 CHAFF 6/14/2010
72 WHEAT 8/20/2002
72 WHEAT 12/7/2007
DESIRED RESULT
ID CODE ENROLL_DATE
================================
01 WHEAT 7/30/2009
72 WHEAT 12/7/2007