Solved

Selecting rows from a atabel based on a a maximum value of a DATE column

Posted on 2009-07-14
4
399 Views
Last Modified: 2013-12-07
Greetings,

I am having a problem doing something which in mind should really be simple but I can't get my head around it.

I have an unormalised  table with 10 columns, 3 of which will comprise the composite primary key.  Now for any two records where this primary key is identical, the other columns may vary, inlcuding a DATE.

What I would like as a result is a table of unique values as far as the primary key goes, and the other columns with values corresponding to the rows for that primary key where the DATE value is MAX.

I've tried coming up with a query which seemed right but I ended up getting a bloody cartesian product :(

This is my table:
ID
REGION
YEAR
CODE
SECTION
UNIT
LOCATION
MOVEMENT
VALUE
ACTION_DATE

Where ID, REGION and YEAR will comprise the primary key and ACTION_DATE is the DATE for which I want the maximum value of, along with the its respective values for the other columns.

Any help would be greatly appreciated!
0
Comment
Question by:FourHorsemen
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
Naveen Kumar earned 50 total points
ID: 24857768
select ID,
REGION,
YEAR,
CODE,
SECTION,
UNIT,
LOCATION,
MOVEMENT,
VALUE,
ACTION_DATE,
max(action_date) over ( partition by id, region,year order by id,region,year )
from your_table_name

but this will get all rows in the table with max action date.

can you provide some sample data and the output you need, so i can be precise with the query.
0
 
LVL 31

Assisted Solution

by:awking00
awking00 earned 50 total points
ID: 24862713
See attached.
query.txt
0
 

Author Comment

by:FourHorsemen
ID: 24871554
Wow...both of those worked great!  Thanks!
0
 

Author Closing Comment

by:FourHorsemen
ID: 31603498
Thank you very much.  That worked perfectly!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Salesforce.com is a cloud-based customer relationship management (CRM) system. In this article, you will learn how to add and map custom lead and contact fields to your Salesforce instance.
In our personal lives, we have well-designed consumer apps to delight us and make even the most complex transactions simple. Many enterprise applications, however, are a bit behind the times. For an enterprise app to be successful in today's tech wo…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now