Link to home
Start Free TrialLog in
Avatar of freezegravity
freezegravity

asked on

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!
Avatar of tim_cs
tim_cs
Flag of United States of America image

;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
ASKER CERTIFIED SOLUTION
Avatar of bitref
bitref
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of freezegravity
freezegravity

ASKER

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

Thanks!