SQL 选择具有多条记录的最大日期
我正在努力查询以获取最新条目。我有一个 Notes 表,其中包含以下列:
BusinessDate
ReportGuid
NoteGuid
Note
NoteDate
NoteAddedBy
BusinessDate、ReportGuid 和 NoteGuid 是表上的 PK。该表允许特定的 ReportGuid 每天有多个注释。我有另一个表,其中包含将加入并为用户显示的附加报告信息。我正在尝试仅提取并显示每个 ReportGuid 的最新注释条目。
我尝试使用 Max(NoteDate) 但这只能让我获得添加到表中的最新注释,而不是每个 ReportGuid 的最新注释。
任何帮助将不胜感激。
谢谢
更新:
感谢您的帮助:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要
按 ReportGuid 分组
并选择Max(NoteDate)
。这将选择每组中的最大值。You need to
group by ReportGuid
and selectMax(NoteDate)
. That will select the maximum of each group.