获取过去24小时内具有匹配数据和最大值

时间:2018-05-01 10:11:20

标签: mysql sql

我有一张下面的结构表。

    CREATE TABLE  notifications (
    `notification_id` int(11) NOT NULL AUTO_INCREMENT,
    `source` varchar(50) NOT NULL,
    `created_time` datetime NOT NULL,
    `not_type` varchar(50) NOT NULL,
    `not_content` longtext NOT NULL,
    `notifier_version` varchar(45) DEFAULT NULL,
    `notification_reason` varchar(245) DEFAULT NULL,
    PRIMARY KEY (`notification_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=50 DEFAULT CHARSET=utf8;

    INSERT INTO `notifications` (`notification_id`,`source`,`created_time`,`not_type`,`not_content`,`notifier_version`,`notification_reason`) VALUES 
    (50,'Asia','2018-05-01 18:10:12','Alert','You are alerted for some Reason','NO_03','Some Reason 1'),
    (51,'Asia','2018-04-29 14:10:12','Alert','You are alerted for some Reason','NO_02','Some Reason 8'),
    (52,'Europe','2018-04-29 10:10:12','Warning','You are Warned for som Reason','NO_02',NULL),
    (53,'Europe','2018-05-01 10:10:12','Warning','You are Warned for som Reason','NO_02',NULL),
    (54,'Europe','2018-04-30 23:10:12','Alert','You are alerted for some Reason','NO_03','Some Reason 1');

我需要收到最新警报的来源列表,过去24小时内收到的警报数以及发送最后警报的通知版本。

我的结果中需要的列是,

  1. 来源 - 表中的不同实例
  2. notification_reason - 最后通知已提升,如果是源24小时之前的事件
  3. notifier_version - 导致源的最后一次警报的Notfier版本
  4. alert_count - 源的最近24个Hors中的警报数。
  5. 我尝试了this SQL Fiddle中的内容。有人可以纠正我并给出解决方案

2 个答案:

答案 0 :(得分:0)

我认为这可以满足您的需求:

select n.source,
       max(case when na.max_ni = n.notification_id then notification_reason end) as last_alert_reason,
       sum(n.not_type = 'Alert') as alert_count,
       max(case when na.max_ni = n.notification_id then notifier_version end) as last_alert_version 
from notifications n left join
     (select n2.source, max(notification_id) as max_ni
      from notifications n2
      where n2.not_type = 'Alert'
      group by n2.source
     ) na
     on n.source = na.source
group by n.source;

SQL小提琴是here

答案 1 :(得分:0)

您可以使用派生表的想法获取过去24小时内的最新ID和计数。

SELECT
  COUNT(`notification_id`) AS AlertCount,
  MAX(`notification_id`) AS MaxNotification
FROM
  `notifications`
WHERE
  `created_time` BETWEEN DATE_SUB(NOW(), INTERVAL 24 HOUR) AND NOW()
  AND `not_type` = 'Alert';

然后,加入并过滤:

SELECT
  NotificationTbl.source,
  NotificationTbl.notification_reason,
  NotificationTbl.notifier_version,
  Last24HoursTbl.alert_count
FROM
  `notifications` AS NotificationTbl
INNER JOIN
  (
   SELECT
      COUNT(`notification_id`) AS alert_count,
      MAX(`notification_id`) AS max_notification_id
    FROM
      `notifications`
    WHERE
      `created_time` BETWEEN DATE_SUB(NOW(), INTERVAL 24 HOUR) AND NOW() 
      AND `not_type` = 'Alert'
  ) AS Last24HoursTbl
  ON NotificationTbl.notification_id = Last24HoursTbl.max_notification_id
  ;

结果(截至答复时间):

source |    notification_reason | notifier_version | alert_count
------------------------------------------------------------
Europe |    Some Reason 1       | NO_03            | 1

SQLFiddle:http://sqlfiddle.com/#!9/14bb6a/14