请您查看以下任务?
我有一个表DATA
,其结构如下:
CREATE TABLE DATA
(
EMPLOYEE nvarchar(50) NOT NULL,
ABSENCE_START nvarchar(50) NOT NULL,
ABSENCE_END nvarchar(50) NOT NULL
)
它包含以下数据:
insert into DATA (EMPLOYEE, ABSENCE_START, ABSENCE_END)
values ('emp01','21/06/2017','22/06/2017'),
('emp01','24/06/2017','27/06/2017'),
('emp02','22/06/2017','24/06/2017'),
('emp02','22/06/2017','27/06/2017')
正如您所看到的那样,' emp02'员工我们有两个缺席期,其中一个(第4行)完全包含另一个缺席期(第3行)。
我需要将这两行合并为一行(第4行)。
预期结果将是:
EMPLOYEE ABSENCE_START ABSENSE
---------------------------------------
emp01 21/06/2017 22/06/2017
emp02 24/06/2017 27/06/2017
emp01 22/06/2017 27/06/2017
提前谢谢!
答案 0 :(得分:0)
如果您要查找的唯一重叠修正是当一行完全包含另一行,并且您没有唯一的行标识符时,您可以使用not exists()
,如下所示:
set dateformat dmy;
;with cte as (
select
employee
, absence_start= convert(date,absence_start)
, absence_end = convert(date,absence_end)
from data
)
select *
from cte d
where not exists (
select 1
from cte i
where i.employee=d.employee
and i.absence_start <= d.absence_start
and i.absence_end >= d.absence_end
and (i.absence_start <> d.absence_start
or i.absence_end <> d.absence_end)
)
rextester演示:http://rextester.com/PHF88226
返回:
+----------+---------------+-------------+
| employee | absence_start | absence_end |
+----------+---------------+-------------+
| emp01 | 2017-06-21 | 2017-06-22 |
| emp01 | 2017-06-24 | 2017-06-27 |
| emp02 | 2017-06-22 | 2017-06-27 |
+----------+---------------+-------------+
答案 1 :(得分:0)
查询应如下所示:
select
*
from dbo.DATA t1
where not exists (
select
*
from dbo.DATA t2
where t1.ABSENCE_START >= t2.ABSENCE_START
and t1.ABSENCE_END < t2.ABSENCE_END)
但是,如果你有重叠的缺席怎么办?像这样:
insert into DATA(EMPLOYEE,ABSENCE_START,ABSENCE_END) values
('emp01','21/06/2017','22/06/2017'),
('emp01','24/06/2017','27/06/2017'),
('emp02','22/06/2017','29/06/2017'),
('emp02','22/06/2017','27/06/2017')