0

I have a table with four columns: Note ID, Contact ID, Created By, and Created Date. I want to run a query that will return the most recent CREATED BY (i.e. "John Smith") by CONTACT ID. Contact ID, naturally, can appear multiple times in the table.

I obviously would normally run a MAX() statement by the Created Date column, but the problem is I need to have the Contact ID & Created By present in the returned data, but I can't group by both of those columns because I would then receive multiple rows per Contact ID/Created By Grouping.

In the example below, what I would like to see returned is: 1118653/Katy Combs, as she has the most recent CreatedOn.

ContactID   Created By         Created Date
1118653     Katy Combs         2020-10-19 19:58:11.687
1118653     Danielle Scarrone   2020-10-15 18:07:01.630
Dale K
  • 25,246
  • 15
  • 42
  • 71

1 Answers1

1

The simple way, do ORDER BY to get the newest rows first. Use TOP 1 to get only one row, the newest one.

select top 1 t.*
from tablename t
order by [Created Date] desc
jarlh
  • 42,561
  • 8
  • 45
  • 63