• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

Grouping SQL Server Query Question

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
freezegravity
Asked:
freezegravity
1 Solution
 
tim_csCommented:
;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
 
bitrefCommented:
Select ContactID, ProgramTypeId, MAX(ReceivedDate)
From Application
Group By ContactID, ProgramTypeId

Open in new window


0
 
freezegravityAuthor Commented:
I ended up using this query as it was easier to understand and gave the results I was expecting.

Thanks!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now