Or you might like ...
Main Topics
Browse All TopicsGiven a example table:
1, 09-21-2008, r01
1, 09-14-2008, r12
1, 07-23-2008, r06
2, 09-22-2008, r08
2, 01-25-2006, r01
3, 11-11-2005, r07
3, 11-13-2007, r03
I would like to retrieve one record for each ID with the max date for that ID. So given the example table I would want to get back three records, one for each ID with the max date for that ID. I have added the code I tried using but it is bringing back a record for each region and ID. I only want the latest ID for each given ID.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
If I change it to have the region field (org_code_r) by itself it gives me more than one region in the result.
SELECT DISTINCT csh.case_gen_nbr,
MAX (csh.status_date),
csh.org_code_r
reg
FROM case_status_hists csh
WHERE SUBSTR (csh.org_code_r, 1, 1) = 'R'
GROUP BY csh.case_gen_nbr, csh.org_code_r
Which you can see in the code snippet below on the second to last record.
It looks for the max region ID with that date, that much is true. You HAVE TO decide which region to choose if there are multiple records with same statdate. Max is only one decision, it could be count, min, avg. With your example data, there is no conflict, and the results will give you the correct region:
1, 09-21-2008, r01
2, 09-22-2008, r08
3, 11-13-2007, r03
As an alternative you could this, as it would give you all regions with the same newest statdate:
mmoore:
The latest one looks good so far, I am testing it out right now. I didn't understand your very first one. It had a "with target as" in front, I've never seen that. When I pasted it into Toad it gave me an error and I didn't know if that was part of the statement or not.
Anyway, I should know within an hour if this new code is working properly. There are 200K records in the table and I need to check it really good. This is for a state level audit.
I will reply by 7:30 EST.
Not sure why the "WITH" syntax did not work for your TOAD. Perhaps you have an older version of TOAD.
In "WITH TARGET", TARGET is simply a temporary table name. I could have called it "COW" or "Azeroth".
"WITH" says ... build a temporary table that looks like this. Then after that, the SELECT part of the statement uses that temporary table. It's not really a "temporary table" in the Oracle sense of the word, but you can think about it that way to visualize what is going on.
If you are going to implement this solution for a regular audit run, I highly suggest you get the the "WITH" example to work or get QLEMO's solution to work. Like I said, the string concatenation solution I gave is really an ugly hack and my mother would probably wash my mouth out with soap if she knew I wrote it.
Thanks for the points.
Business Accounts
Answer for Membership
by: QlemoPosted on 2008-10-06 at 09:20:14ID: 22651678
Are you interested in the region code? If not, just omit it. Else you have to do something like
Select allOpen in new window