在MYSQL中将日期转换为日期范围---如何处理日期中的差距

时间:2017-07-10 23:46:59

标签: mysql date gaps-and-islands

我正在寻找帮助将以下数据转换为所需的输出。我们在Item,LOC DAY级别数据中有数据需要转换为Item,Loc Date Range以减少表格中的记录数量以及其他要求。

Item        LOC  RP_DATE    RP_IND   
1003785256  543 2016-11-05  Y
1003785256  543 2016-11-06  Y
1003785256  543 2016-11-07  Y
1003785256  543 2016-11-09  Y
1003785256  543 2016-11-10  Y
1003790365  150 2016-11-05  Y
1003797790  224 2016-11-05  Y
1003797790  224 2016-11-06  Y
1003797790  224 2016-11-07  Y
1003797790  224 2016-11-08  Y

必需输出:

Item        LOC  RP_ST_DATE    RP_END_DATE   
1003785256  543 2016-11-05   2016-11-07
1003785256  543 2016-11-09   2016-11-10
1003790365  150 2016-11-05   2016-11-05
1003797790  224 2016-11-05   2016-11-08

1 个答案:

答案 0 :(得分:1)

这种方法适用于MySQL。它使用有序子查询中的组合变量来为每个"范围"建立一个共同的开始日期。 CROSS JOIN仅用于初始化变量,它不会改变行数。一旦建立了共同的开始日期,它就会在外部查询中通过查询变为一个简单的组。

SELECT Item, LOC, RP_IND, dr_begin, MAX(RP_DATE) dr_end
FROM (
  SELECT
         mytable.*
       , @fin := CONVERT(IF(@item<=>item AND @loc<=>loc AND DATEDIFF(rp_date, @d)=1, @fin, rp_date), DATE) AS dr_begin
       , @item := item
       , @loc := loc
       , @d := rp_date
  FROM     mytable CROSS JOIN (SELECT @item:=NULL, @loc:=NULL, @d:=NULL, @fin := NULL) AS init
  ORDER BY item, loc, rp_date
  ) d
GROUP BY  Item, LOC, RP_IND, dr_begin
;

+----+------------+-----+--------+------------+---------------------+
|    |    Item    | LOC | RP_IND |  dr_begin  |       dr_end        |
+----+------------+-----+--------+------------+---------------------+
|  1 | 1003785256 | 543 | Y      | 2016-11-05 | 07.11.2016 00:00:00 |
|  2 | 1003785256 | 543 | Y      | 2016-11-09 | 10.11.2016 00:00:00 |
|  3 | 1003790365 | 150 | Y      | 2016-11-05 | 05.11.2016 00:00:00 |
|  4 | 1003797790 | 224 | Y      | 2016-11-05 | 08.11.2016 00:00:00 |
+----+------------+-----+--------+------------+---------------------+

注意&lt; =&gt; returns 1 if both operands are NULL

查看位于http://rextester.com/SEYG96251

的查询
#drop table mytable;

CREATE TABLE mytable(
   Item    INTEGER  NOT NULL
  ,LOC     INTEGER  NOT NULL
  ,RP_DATE DATE  NOT NULL
  ,RP_IND  VARCHAR(1) NOT NULL
);
INSERT INTO mytable(Item,LOC,RP_DATE,RP_IND) VALUES (1003785256,543,'2016-11-05','Y');
INSERT INTO mytable(Item,LOC,RP_DATE,RP_IND) VALUES (1003785256,543,'2016-11-06','Y');
INSERT INTO mytable(Item,LOC,RP_DATE,RP_IND) VALUES (1003785256,543,'2016-11-07','Y');
INSERT INTO mytable(Item,LOC,RP_DATE,RP_IND) VALUES (1003785256,543,'2016-11-09','Y');
INSERT INTO mytable(Item,LOC,RP_DATE,RP_IND) VALUES (1003785256,543,'2016-11-10','Y');
INSERT INTO mytable(Item,LOC,RP_DATE,RP_IND) VALUES (1003790365,150,'2016-11-05','Y');
INSERT INTO mytable(Item,LOC,RP_DATE,RP_IND) VALUES (1003797790,224,'2016-11-05','Y');
INSERT INTO mytable(Item,LOC,RP_DATE,RP_IND) VALUES (1003797790,224,'2016-11-06','Y');
INSERT INTO mytable(Item,LOC,RP_DATE,RP_IND) VALUES (1003797790,224,'2016-11-07','Y');
INSERT INTO mytable(Item,LOC,RP_DATE,RP_IND) VALUES (1003797790,224,'2016-11-08','Y');