查找最新事件时间在指定时间段内的任何行

发布于 2024-08-14 21:28:12 字数 541 浏览 4 评论 0原文

这与 OpenNMS 相关,我正在尝试用 SQL 编写自动化。我有一个系统,每次备份成功时都会将事件发送到表。我想检测任何给定节点的备份是否过期。因此,例如(简化的)表如下所示:

nodeid, eventid, eventuei, eventtime
  1   ,  1     , backupOk, 09:20 15/12/09
  2   ,  2     , backupOk, 09:25 15/12/09
  3   ,  3     , backupOk, 09:30 15/12/09
  1   ,  4     , backupOk, 09:20 16/12/09
  2   ,  5     , backupOk, 09:25 16/12/09
  2   ,  6     , backupOk, 09:25 17/12/09
  3   ,  7     , backupOk, 09:30 17/12/09

所以我需要的是备份逾期 24 小时的节点 ID 列表(我猜根本没有发生备份,尽管该数据库中有些节点没有发生备份) t 得到备份(因为它们是不同类型的节点))。

The relates to OpenNMS where I'm trying to write an automation in SQL. I have a system which sends events to a table every time the backup succeeds. I want to detect if the backup is overdue for any given node. So for example the (simplified) table looks like:

nodeid, eventid, eventuei, eventtime
  1   ,  1     , backupOk, 09:20 15/12/09
  2   ,  2     , backupOk, 09:25 15/12/09
  3   ,  3     , backupOk, 09:30 15/12/09
  1   ,  4     , backupOk, 09:20 16/12/09
  2   ,  5     , backupOk, 09:25 16/12/09
  2   ,  6     , backupOk, 09:25 17/12/09
  3   ,  7     , backupOk, 09:30 17/12/09

So what I need is a list of nodeid's where the backup is overdue by 24 hours (and I guess where no backup has occurred at all, although there are nodes in this database that don't get backed up (as they are different types of node)).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

半岛未凉 2024-08-21 21:28:12

从您向我们展示的架构中获取未备份的节点列表是不可能的。

获取逾期 24 小时的节点列表很简单:

select nodeid, max(eventtime)
from your_table
group by nodeid
having max(eventtime) < now() - '24 hours'::interval

Getting list of nodes that were not backed up - from your schema that you showed us - is not possible.

Getting list of nodes that are 24 hours overdue is trivial:

select nodeid, max(eventtime)
from your_table
group by nodeid
having max(eventtime) < now() - '24 hours'::interval
南烟 2024-08-21 21:28:12

以下 SQL 适用于 Oracle,但我假设 PostgreSQL 中也有类似的时间处理工具:

SELECT E.NODEID, E.LAST_BACKUP_TIME
  FROM (SELECT NODEID, MAX(EVENTTIME) AS LAST_BACKUP_TIME
          FROM BACKUP_EVENTS
          WHERE EVENTUEI = 'backupOk'
          GROUP BY NODEID) E
  WHERE E.LAST_BACKUP_TIME < SYSDATE - INTERVAL '2' DAY;

这将报告过去 48 小时内未运行备份的节点。

分享并享受。

The following SQL is for Oracle but I'll assume there are similar time-handling facilities in PostgreSQL:

SELECT E.NODEID, E.LAST_BACKUP_TIME
  FROM (SELECT NODEID, MAX(EVENTTIME) AS LAST_BACKUP_TIME
          FROM BACKUP_EVENTS
          WHERE EVENTUEI = 'backupOk'
          GROUP BY NODEID) E
  WHERE E.LAST_BACKUP_TIME < SYSDATE - INTERVAL '2' DAY;

This will report on nodes where a backup hasn't been run in the past 48 hours.

Share and enjoy.

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