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



我正在寻找将以下数据转换为所需输出的帮助。我们有项目,LOC DAY级别的数据,需要转换为项目,LOC日期范围,以减少表中的记录数量和其他要求。

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

这种方法适用于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 |
+----+------------+-----+--------+------------+---------------------+

注意<=>如果两个操作数均为 NULL 则返回 1

请参阅以下位置的查询: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');

相关内容

  • 没有找到相关文章

最新更新