没有Window功能的下一行值

时间:2019-01-25 08:48:04

标签: mysql sql

我有下表页面状态更新。每行都是状态更改:

CREATE TABLE `t1` (
  `page_id` int(11) NOT NULL,
  `country` varchar(2) COLLATE utf8mb4_unicode_ci NOT NULL,
  `status` varchar(45) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `date` datetime DEFAULT NULL,
  PRIMARY KEY (`page_id`,`country`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO t1        (page_id, country, status, date) 
VALUES                (1, 'de', 'deactivated', '2018-09-28 14:52:11' ) ,
                      (1, 'de', 'activated', '2018-09-28 14:54:18' ) ,
                      (1, 'de', 'deactivated', '2018-09-28 14:60:12' ) ,
                      (1, 'de', 'moderated', '2018-09-28 14:54:12' ) ,
                      (2, 'es', 'deactivated', '2018-09-28 14:52:01' ) ,
                      (2, 'es', 'activated', '2018-09-28 14:52:07' ) ,
                      (2, 'es', 'deactivated', '2018-09-28 14:52:11' ) 

我想以这种格式查看表格,以便直到页面具有特定状态时才显示每一行:

CREATE TABLE `t2` (
  `page_id` int(11) NOT NULL,
  `country` varchar(2) COLLATE utf8mb4_unicode_ci NOT NULL,
  `status` varchar(45) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `date_from` datetime DEFAULT NULL,
  `date_to` datetime DEFAULT NULL,
  PRIMARY KEY (`page_id`,`country`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;



INSERT INTO t2        (page_id, country, status, date_from, date_to) 
VALUES                (1, 'de', 'deactivated', '2018-09-28 14:52:11', '2018-09-28 14:54:12'  ) ,
                      (1, 'de', 'moderated', '2018-09-28 14:54:12', '2018-09-28 14:54:18'  ) ,
                      (1, 'de', 'activated', '2018-09-28 14:54:18','2018-09-28 14:60:12'  ) ,
                      (1, 'de', 'deactivated', '2018-09-28 14:60:12','2018-01-25 14:60:12' ) ,
                      (2, 'es', 'deactivated', '2018-09-28 14:52:01','2018-09-28 14:52:07'  ) ,
                      (2, 'es', 'activated', '2018-09-28 14:52:07', '2018-09-28 14:52:11' ) ,
                      (2, 'es', 'deactivated', '2018-09-28 14:52:11','2018-01-25 14:52:11'  ) ;

问题是我们仍在使用MySQL 5.7,还没有升级到具有cte和window函数的mysql8,这很容易解决问题:

  SELECT
         country,
         page_id,
         status,
         date as date_from,
        COALESCE(MIN(date) OVER(PARTITION BY country, page_id ORDER BY date DESC
                            ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING),
                        TIMESTAMPADD(day, 1, current_timestamp())) as date_to
  FROM t1 ;

我认为应该有一个自我连接的技巧,但无法弄清楚到底是什么!

1 个答案:

答案 0 :(得分:1)

在MySQL 8+中,通常为此使用LEAD(),而不是复杂的表达式。

在早期版本中,您可以使用相关子查询:

select t1.*,
       (select tt1.date
        from t1 tt1
        where tt1.country = t1.country and tt1.page_id = t1.page_id and
              tt1.date > t1.date 
        order by tt1.date asc
        limit 1
       ) as date_to
from t1;

这在每个序列末尾的NULL值中使用date_to。这对我来说更有意义。

如果您想重复date_from(我不建议这样做),则可以执行以下操作:

select t1.*,
       (select min(tt1.date_from, t1.date_from)
        from t1 tt1
        where tt1.page_id = t1.page_id and
              tt1.date_from > t1.date_from
       ) as date_to
from t1;