SQL 选择具有多条记录的最大日期

发布于 2024-10-14 21:00:27 字数 663 浏览 1 评论 0原文

我正在努力查询以获取最新条目。我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2024-10-21 21:00:27

您需要按 ReportGuid 分组并选择Max(NoteDate)。这将选择每组中的最大值。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文