Sql 插入5列。我们遗漏了什么吗?@Larnu谢谢你提到这一点,我已经更新了。 declare @source table( id int not null, serviceId int not null, startDate datet
Sql 插入5列。我们遗漏了什么吗?@Larnu谢谢你提到这一点,我已经更新了。 declare @source table( id int not null, serviceId int not null, startDate datet,sql,sql-server,Sql,Sql Server,插入5列。我们遗漏了什么吗?@Larnu谢谢你提到这一点,我已经更新了。 declare @source table( id int not null, serviceId int not null, startDate datetime, endDate datetime, [date] date ) insert into @source values (1, 4, '2020-03-16 07:00:00.000', '2020-03-16 10
插入
5列。我们遗漏了什么吗?@Larnu谢谢你提到这一点,我已经更新了。
declare @source table(
id int not null,
serviceId int not null,
startDate datetime,
endDate datetime,
[date] date
)
insert into @source values (1, 4, '2020-03-16 07:00:00.000', '2020-03-16 10:00:00.000', '2020-03-16')
insert into @source values (2, 4, '2020-03-16 11:00:00.000', '2020-03-16 16:00:00.000', '2020-03-16')
insert into @source values (3, 4426, '2020-03-16 08:00:00.000', '2020-03-16 16:00:00.000', '2020-03-16')
insert into @source values (4, 91, '2020-03-16 15:00:00.000', '2020-03-16 17:00:00.000', '2020-03-16')
select
id
, serviceId
, startDate
, endDate
, [date]
from @source
2020-03-16 15:00:00.000 - 2020-03-16 16:00:00.000
declare @source table(
id int not null,
serviceId int not null,
startDate datetime,
endDate datetime,
[date] date
)
insert into @source values (1, 4, '2020-03-16 07:00:00.000', '2020-03-16 10:00:00.000', '2020-03-16')
insert into @source values (2, 4, '2020-03-16 11:00:00.000', '2020-03-16 16:00:00.000', '2020-03-16')
select
id
, serviceId
, startDate
, endDate
, [date]
from @source
2020-03-16 07:00:00.000 - 2020-03-16 10:00:00.000
2020-03-16 11:00:00.000 - 2020-03-16 16:00:00.000
declare @source table(
id int not null,
serviceId int not null,
startDate datetime,
endDate datetime,
[date] date
)
insert into @source values (1, 4, '2020-03-16 07:00:00.000', '2020-03-16 10:00:00.000', '2020-03-16')
insert into @source values (2, 4, '2020-03-16 11:00:00.000', '2020-03-16 16:00:00.000', '2020-03-16')
----Uncommenting causes no results, which is wrong
--insert into @source values (3, 4426, '2020-03-16 08:00:00.000', '2020-03-16 16:00:00.000', '2020-03-16')
--insert into @source values (4, 91, '2020-03-16 15:00:00.000', '2020-03-16 17:00:00.000', '2020-03-16')
select
openinghours.[date]
, openinghours.[from] as [from]
, openinghours.[to] as [to]
from
(
select
openinghours_with_comparison.[date]
, openinghours_with_comparison.[from1]
, openinghours_with_comparison.[to1]
, max(openinghours_with_comparison.[has_overlap]) as overlap
, case
when max(openinghours_with_comparison.[has_overlap]) = 1 then max(openinghours_with_comparison.[new_from])
else openinghours_with_comparison.[from1]
end as [from]
, case
when max(openinghours_with_comparison.[has_overlap]) = 1 then min(openinghours_with_comparison.[new_to])
else openinghours_with_comparison.[to1]
end as [to]
from
(
select
openinghours.[date]
, openinghours.[serviceId]
, openinghours_to_compare.[serviceId] as [compare_service_id]
, openinghours.[startDate] as [from1]
, openinghours.[endDate] as [to1]
, openinghours_to_compare.[startDate] as [from2]
, openinghours_to_compare.[endDate] as [to2]
, case
when
openinghours.[serviceId] != openinghours_to_compare.[serviceId] and
(
(
openinghours.[startDate] != openinghours_to_compare.[startDate]
and
(
openinghours.[startDate] between openinghours_to_compare.[startDate] and openinghours_to_compare.[endDate]
or
openinghours.[endDate] between openinghours_to_compare.[startDate] and openinghours_to_compare.[endDate]
)
)or
(
openinghours.[endDate] != openinghours_to_compare.[endDate]
and
(
openinghours_to_compare.[startDate] between openinghours.[startDate] and openinghours.[endDate]
or
openinghours_to_compare.[endDate] between openinghours.[startDate] and openinghours.[endDate]
)
)
) then 1
else 0
end as [has_overlap]
, case
when (openinghours.[startDate] != openinghours_to_compare.[startDate] and openinghours.[startDate] between openinghours_to_compare.[startDate] and openinghours_to_compare.[endDate]) then openinghours.[startDate]
else openinghours_to_compare.[startDate]
end as [new_from]
, case
when (openinghours.[endDate] != openinghours_to_compare.[endDate] and openinghours.[endDate] between openinghours_to_compare.[startDate] and openinghours_to_compare.[endDate]) then openinghours.[endDate]
else openinghours_to_compare.[endDate]
end as [new_to]
from
(
select
*
from @source
) openinghours
left join
(
select
*
from @source
)openinghours_to_compare on openinghours_to_compare.[date] = openinghours.[date]
)openinghours_with_comparison
group by
openinghours_with_comparison.[date]
, openinghours_with_comparison.[serviceId]
, openinghours_with_comparison.[from1]
, openinghours_with_comparison.[to1]
)openinghours
group by
openinghours.[date]
, openinghours.[from]
, openinghours.[to]
having count(*) = (select count(distinct serviceId) from @source)
and openinghours.[from] <= openinghours.[to]