MySQL合并功能无法正常工作

时间:2016-08-15 21:44:03

标签: mysql

以下查询未返回正确的 startDowntime 。我最近将数据库迁移到最新的MYSQL版本。我以前的数据库版本中相同的查询工作正常。

startDowntime 应显示介于该范围之间的任何内容或返回null。我收到的实际输出不在范围内。它返回DB中可用的最小DateTime。

SELECT a.service_name, a.status,
   (CASE 
     WHEN a.status = "Down" THEN a.time_stamp 
     WHEN a.status = "Up" THEN COALESCE(
       (
         select b.time_stamp from service_status b
         where b.status = "Down" and b.time_stamp < a.time_stamp 
           and b.time_stamp >= '2016-08-07 22:00' 
           and b.service_name = a.service_name 
           and b.environment = a.environment
         order by b.time_stamp desc 
         limit 1
       ) , null 
     ) END
   ) AS  startDowntime,
   (CASE
     WHEN a.status = "Up" THEN a.time_stamp
     WHEN a.status = "Down" THEN COALESCE(
       (
         select b.time_stamp from service_status b
         where b.status = "Up" and b.time_stamp > '2016-08-07 22:00' 
           and b.service_name = a.service_name 
           and b.environment = a.environment
         order by b.time_stamp asc limit 1
       ), NOW()
     ) END
   ) AS endDowntime
FROM service_status a
WHERE a.time_stamp >= '2016-08-07 22:00' 
  AND a.time_stamp <= '2016-08-12 18:00' 
  AND a.environment = 'QA3'
GROUP BY a.service_name, startDowntime

表定义

CREATE TABLE service_status( time_stamp timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL, environment varchar(20)NOT NULL, service_name varchar(40)NOT NULL, interface_name varchar(40)NOT NULL, description varchar(100), monitoring_method varchar(50), status varchar(10), is_stub varchar(10)DEFAULT N, CONSTRAINT PRIMARY PRIMARY KEY(time_stamp,environment,service_name)  );

在service_status上创建UNIQUE INDEX PRIMARY(time_stamp,environment,service_name); CREATE UNIQUE INDEX PRIMARY_IDX001 ON service_status(time_stamp,environment,service_name,interface_name);

预期产出

status|service_name|startDowntime|endDowntime  
------|------------|-------------|-----------  
Up    |COUPONVAL   |null         |2016-08-09 12:45:01.0

实际输出

status|service_name|startDowntime         |endDowntime
------|------------|----------------------|-----------  
Up    |COUPONVAL   |2014-09-23 10:32:00.0 |2016-08-09 12:45:01.0

表定义是

CREATE TABLE service_status (    time_stamp timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,    environment varchar(20)NOT NULL,    service_name varchar(40)NOT NULL,    interface_name varchar(40)NOT NULL,    description varchar(100),    monitoring_method varchar(50),    status varchar(10),    is_stub varchar(10)DEFAULT N,    CONSTRAINT PRIMARY PRIMARY KEY(time_stamp,environment,service_name) ) ; 在service_status上创建UNIQUE INDEX PRIMARY (   TIME_STAMP,   环境,   服务名称 ) ; 在service_status上创建UNIQUE INDEX PRIMARY_IDX001 (   TIME_STAMP,   环境,   服务名称,   接口名称 ) ;

0 个答案:

没有答案