Taryn
Taryn

Reputation: 247690

SQL Select Max Date with Multiple records

I am struggling with a query to pull most recent entries. I have a Notes table that contains the following columns:

BusinessDate
ReportGuid
NoteGuid
Note
NoteDate
NoteAddedBy

The BusinessDate, ReportGuid and NoteGuid are the PK on the table. This table allows a specific ReportGuid to have multiple notes per day. I have another table that contains additional Report info that will be joined and displayed for the users. I am trying to pull and display only the most recent note entry for each ReportGuid.

I tried using Max(NoteDate) but that is only getting me the latest note added to the table not the latest note for each ReportGuid.

Any help would be appreciated.

Thanks

UPDATE:

thanks for the help:

SELECT N.Note, N.ReportGuid
FROM Tracking.SM_T_Report_Notes N
RIGHT OUTER JOIN
    (
    SELECT ReportGuid, Max(NoteDate) As NoteDate
    FROM Tracking.SM_T_Report_Notes
    GROUP BY ReportGuid
    ) AS ND
    ON  N.NoteDate = ND.NoteDate

Upvotes: 8

Views: 65170

Answers (1)

Adrian Grigore
Adrian Grigore

Reputation: 33318

You need to group by ReportGuid and select Max(NoteDate). That will select the maximum of each group.

Upvotes: 12

Related Questions