SQL选择具有多个记录的最大日期 [英] SQL Select Max Date with Multiple records

查看:149
本文介绍了SQL选择具有多个记录的最大日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力查询以吸引最近的条目。我有一个Notes表,其中包含以下列:

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

BusinessDate,ReportGuid和NoteGuid是PK桌子。该表允许特定的ReportGuid每天有多个笔记。我有另一个表格包含额外的报告信息,将被加入并显示给用户。我试图拉出并显示每个ReportGuid的最新笔记条目。

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.

我尝试使用Max(NoteDate),但这只是让我添加了最新的笔记该表不是每个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.

任何帮助将不胜感激。

谢谢

更新:

感谢您的帮助:

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


推荐答案

您需要通过ReportGuid group并选择 Max(NoteDate)。这将选择每个组的最大值。

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

这篇关于SQL选择具有多个记录的最大日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆