如何从Mysql中的表中获取最新状态

时间:2017-07-04 09:09:38

标签: mysql

下面是表格描述和插入脚本以将数据加载到表格中。我正在开发Aurora Mysql数据库。

 DROP TABLE IF EXISTS batch;

 CREATE TABLE `Batch` (
      `ID` varchar(50) DEFAULT NULL,
      `StepID` varchar(50) DEFAULT NULL,
      `FileName` varchar(200) DEFAULT NULL,
      `StartDateTime` datetime DEFAULT NULL,
      `EndDateTime` datetime DEFAULT NULL,
      `Status` varchar(50) DEFAULT NULL,
      `ETLLoadDate` datetime NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

以下是我获得两个成功的两个场景。理想情况下,10的状态失败和20的成功。

插入批次值(10,10.1,'a.csv','2017-07-06 11:16:07','2017-07-06 11:17:07','FAILED','2017 -07-06 11:17:07'); 插入批次值(10,10.1,'b.csv','2017-07-06 11:18:08','2017-07-06 11:19:07','SUCCESS','2017-07- 06 11:19:07'); 插入批次值(10,10.1,'c.csv','2017-07-06 11:19:08','2017-07-06 11:20:07','SUCCESS','2017-07- 06 11:20:07'); 插入批次值(10,10.1,'d.csv','2017-07-06 11:20:08','2017-07-06 11:21:07','SUCCESS','2017-07- 06 11:21:07'); 插入批次值(10,10.2,'e.csv','2017-07-06 11:21:08','2017-07-06 11:22:07','SUCCESS','2017-07- 06 11:22:07'); 插入批次值(10,10.3,'f.csv','2017-07-06 11:22:08','2017-07-06 11:23:07','SUCCESS','2017-07- 06 11:23:07');

插入批次值(20,20.1,'a.csv','2017-07-06 11:16:07','2017-07-06 11:17:07','FAILED','2017 -07-06 11:17:07'); 插入批次值(20,20.1,'b.csv','2017-07-06 11:18:08','2017-07-06 11:19:07','SUCCESS','2017-07- 06 11:19:07'); 插入批次值(20,20.1,'c.csv','2017-07-06 11:19:08','2017-07-06 11:20:07','SUCCESS','2017-07- 06 11:20:07'); 插入批次值(20,20.1,'d.csv','2017-07-06 11:20:08','2017-07-06 11:21:07','SUCCESS','2017-07- 06 11:21:07'); 插入批次值(20,20.2,'e.csv','2017-07-06 11:21:08','2017-07-06 11:22:07','SUCCESS','2017-07- 06 11:22:07'); 插入批次值(20,20.3,'f.csv','2017-07-06 11:22:08','2017-07-06 11:23:07','SUCCESS','2017-07- 06 11:23:07'); 插入批次值(20,20.1,'a.csv','2017-07-06 11:16:07','2017-07-06 11:17:07','SUCCESS','2017-07- 06 11:17:07');

1 个答案:

答案 0 :(得分:0)

E.g。

SELECT DISTINCT DATE(a.startdatetime)
              , COALESCE(b.status,a.status) status 
  FROM 
     ( SELECT x.* 
         FROM batch x
         JOIN
            ( SELECT id
                   , stepid
                   , DATE(startdatetime) dt
                   , MAX(startdatetime) max_dt
                FROM batch
               GROUP
                  BY id
                   , stepid
                   , dt
            ) y
           ON y.id = x.id
          AND y.stepid = x.stepid
          AND y.dt = DATE(x.startdatetime)
          AND y.max_dt = x.startdatetime
     ) a
  LEFT
  JOIN
     ( SELECT x.* 
         FROM batch x
         JOIN
            ( SELECT id
                   , stepid
                   , DATE(startdatetime) dt
                   , MAX(startdatetime) max_dt
                FROM batch
               GROUP
                  BY id
                   , stepid
                   , dt
            ) y
           ON y.id = x.id
          AND y.stepid = x.stepid
          AND y.dt = DATE(x.startdatetime)
          AND y.max_dt = x.startdatetime
     ) b
    ON b.id = a.id
   AND DATE(b.startdatetime) = DATE(a.startdatetime)
   AND b.status = 'failed'
 WHERE a.status = 'success';
+-----------------------+---------+
| DATE(a.startdatetime) | status  |
+-----------------------+---------+
| 2017-06-21            | SUCCESS |
| 2017-06-22            | FAILED  |
| 2017-06-24            | FAILED  |
| 2017-06-23            | SUCCESS |
+-----------------------+---------+