Solved

Grouping SQL Server Query Question

Posted on 2011-09-26
3
191 Views
Last Modified: 2012-05-12
Hello experts,

I have this table structure

Application
ID, ContactID, ProgramTypeId, ReceivedDate, OtherMetadata

I want to get only the most recent ReceivedDate for a given ContactID, ProgramTypeId combination.

Let me know if you would like me to add sample data.

Thanks!
0
Comment
Question by:freezegravity
3 Comments
 
LVL 15

Expert Comment

by:tim_cs
Comment Utility
;WITH CTE AS (
SELECT
   ContactID
   ,ProgramTypeID
   ,ReceivedDate
   ,ROW_NUMBER() OVER (Partition By ContactID, ProgramTypeID ORDER BY ReceivedDate DESC) RN
FROM
   Application
)

SELECT
   *
FROM
   CTE
WHERE
   RN = 1
0
 
LVL 5

Accepted Solution

by:
bitref earned 500 total points
Comment Utility
Select ContactID, ProgramTypeId, MAX(ReceivedDate)
From Application
Group By ContactID, ProgramTypeId

Open in new window


0
 

Author Closing Comment

by:freezegravity
Comment Utility
I ended up using this query as it was easier to understand and gave the results I was expecting.

Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

763 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

14 Experts available now in Live!

Get 1:1 Help Now