我正在使用MySQL 5.6
我正在解决一个复杂的业务问题。
无论如何,我尝试使用这个例子来轻松解释问题。
有一张包含这些记录的表格:
Acct-1 2016-01-01 2016-01-07
Acct-1 2016-01-05 2016-01-11
Acct-1 2016-01-18 2016-01-24
Acct-1 2016-02-02 2016-02-08
Acct-1 2016-02-07 2016-02-13
第一列是帐号。在此示例中,所有帐号均相同。第2列和第3列是日期范围。所有日期范围恰好是7天。
我想让这张桌子更美观,将重叠范围“合并”到一个日期范围内。
因此,record-1和record-2应合并在一起成为:
Acct-1 2016-01-01 2016-01-11
Record-3是独立的,无需与其他人合并。
Record-4和record-5应合并在一起成为:
Acct-1 2016-02-02 2016-02-13
最后,每个帐户都应包含非重叠日期范围的记录。
注意,有时我们必须合并2或3或4或其他记录,具体取决于重叠。表中有许多不同的帐号。
如何使用SQL语句?
可以创建临时视图以达到目的。
干杯,
阿尔文
答案 0 :(得分:0)
这是一个使用用户变量的解决方案。通过您提供的数据进行验证,我插入了更多数据进行验证。
要永久保存查询结果并在以后保持可访问状态,结果将写入表tbl_new。
完整演示>>
SQL:
-- data preparation
create table tbl(acct varchar(100), start_date date, end_date date);
insert into tbl values
('Acct-1', '2016-01-01', '2016-01-07'),
('Acct-1', '2016-01-05', '2016-01-11'),
('Acct-1', '2016-01-18', '2016-01-24'),
('Acct-1', '2016-02-02', '2016-02-08'),
('Acct-1', '2016-02-07', '2016-02-13'),
('Acct-2', '2016-01-01', '2016-01-07'),
('Acct-2', '2016-01-18', '2016-01-24'),
('Acct-2', '2016-01-02', '2016-02-08');
SELECT * FROM tbl;
-- Need queries
SET @last_acct = '', @last_start_date = '1970-01-01', @last_end_date = '9999-12-31', @group = 1;
CREATE TABLE tbl_new (acct varchar(100), start_date date, end_date date);
INSERT INTO tbl_new
SELECT
acct,
MIN(start_date) start_date,
MAX(end_date) end_date
FROM
(
SELECT
acct,
start_date,
end_date,
CASE
WHEN
acct = @last_acct AND start_date <= @last_end_date
THEN
@group
ELSE
@group := @group + 1
END group_num,
@last_acct := acct,
@last_start_date := start_date,
@last_end_date := end_date
FROM
tbl
) tbl2
GROUP BY group_num;
SELECT * FROM tbl_new;
输出:
mysql> SELECT * FROM tbl;
+--------+------------+------------+
| acct | start_date | end_date |
+--------+------------+------------+
| Acct-1 | 2016-01-01 | 2016-01-07 |
| Acct-1 | 2016-01-05 | 2016-01-11 |
| Acct-1 | 2016-01-18 | 2016-01-24 |
| Acct-1 | 2016-02-02 | 2016-02-08 |
| Acct-1 | 2016-02-07 | 2016-02-13 |
| Acct-2 | 2016-01-01 | 2016-01-07 |
| Acct-2 | 2016-01-18 | 2016-01-24 |
| Acct-2 | 2016-01-02 | 2016-02-08 |
+--------+------------+------------+
8 rows in set (0.00 sec)
mysql>
mysql> -- Need queries
mysql> SET @last_acct = '', @last_start_date = '1970-01-01', @last_end_date = '9999-12-31', @group = 1;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> CREATE TABLE tbl_new (acct varchar(100), start_date date, end_date date);
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql> INSERT INTO tbl_new
-> SELECT
-> acct,
-> MIN(start_date) start_date,
-> MAX(end_date) end_date
-> FROM
-> (
-> SELECT
-> acct,
-> start_date,
-> end_date,
-> CASE
-> WHEN
-> acct = @last_acct AND start_date <= @last_end_date
-> THEN
-> @group
-> ELSE
-> @group := @group + 1
-> END group_num,
-> @last_acct := acct,
-> @last_start_date := start_date,
-> @last_end_date := end_date
-> FROM
-> tbl
-> ) tbl2
-> GROUP BY group_num;
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql>
mysql> SELECT * FROM tbl_new;
+--------+------------+------------+
| acct | start_date | end_date |
+--------+------------+------------+
| Acct-1 | 2016-01-01 | 2016-01-11 |
| Acct-1 | 2016-01-18 | 2016-01-24 |
| Acct-1 | 2016-02-02 | 2016-02-13 |
| Acct-2 | 2016-01-01 | 2016-01-07 |
| Acct-2 | 2016-01-02 | 2016-02-08 |
+--------+------------+------------+
5 rows in set (0.00 sec)