Solved

Selecting all records that have a max value in another column?

Posted on 2008-11-02
4
398 Views
Last Modified: 2009-07-18
Hi there,
I have a table where I want to retrieve all the records that have a maximum modified date for a particular fact:

FACT_ID, FACT1, MODIFIED_DATE ... (100 other facts)
1, A, 2008-01-01 00:00:00.000
2, A, 2008-02-01 00:00:00.005
3, A, 2008-02-01 00:00:00.005
4, B, 2007-01-01 00:00:00.000
5, B, 2008-05-05 00:00:00.000

I would like the query to return rows 2, 3, and 5.. I've only been able to do this using a cursor so far iterating through all the fact_ids, but I'm sure there must be a set operation that can do this :)

Thanks,

Charles
0
Comment
Question by:cshlin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 9

Expert Comment

by:jamesgu
ID: 22863606
select * from  a,
inner join (select FACT_ID, FACT1, max(MODIFIED_DATE) as max_MODIFIED_DATE from  group by FACT_ID, FACT1) b
where a.FACT_ID  = b. FACT_ID
and a. FACT1  = b. FACT1  
and a. MODIFIED_DATE = b. max_MODIFIED_DATE
0
 
LVL 9

Accepted Solution

by:
jamesgu earned 63 total points
ID: 22863658
try this one instead of the previous

select * from  a,
inner join (select FACT1, max(MODIFIED_DATE) as max_MODIFIED_DATE from  group by FACT1) b
where a.FACT_ID  = b. FACT_ID
and a. MODIFIED_DATE = b. max_MODIFIED_DATE
0
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 62 total points
ID: 22863851
Try this:

with RankedData as
(select *,dense_rank() over(partition by fact1 order by modified_date desc) dr from YourTable)
select * from RankedData
where dr=1
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

705 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