问题:
给定一个表格,其中某个特定时间段可能(或可能不)重叠并给出另一个包含排除的表格,我想计算每个人和期间的天数,不包括重叠天数和排除周期。
正如他们所说的那样,它的价值是1000字,所以:
datediff("d", end_date, start_date)
示例方案:
期间表:
create table periods (
`id` COUNTER (1,1) PRIMARY KEY,
`person_id` text(50),
`start_date` Date,
`end_date` Date
)
排除期间表:
create table exclusions (
`start_date` As Date,
`end_date` As Date
)
还有一些价值观:
INSERT INTO `periods`(`person_id`, `start_date`, `end_date`)
VALUES('1', CDate('01/09/2014'), CDate('30/09/2014'));
INSERT INTO `periods`(`person_id`, `start_date`, `end_date`)
VALUES('1', CDate('10/10/2014'), CDate('31/10/2014'));
INSERT INTO `periods`(`person_id`, `start_date`, `end_date`)
VALUES('1', CDate('25/09/2014'), CDate('15/10/2014'));
INSERT INTO `periods`(`person_id`, `start_date`, `end_date`)
VALUES('1', CDate('20/11/2014'), CDate('10/12/2014'));
INSERT INTO `periods`(`person_id`, `start_date`, `end_date`)
VALUES('1', CDate('15/11/2014'), CDate('25/11/2014'));
INSERT INTO `exclusions`(`start_date`, `end_date`)
VALUES(CDate('10/09/2014'), CDate('15/09/2014'));
INSERT INTO `exclusions`(`start_date`, `end_date`)
VALUES(CDate('01/12/2014'), CDate('20/12/2014'));
我尝试了什么:
到目前为止,我能够使用此查询检测重叠的时段:
SELECT s1.person_id as person_id,
iif(s1.start_date <= s2.start_date, s1.start_date, s2.start_date) As start_date,
iif(s1.end_date >= s2.end_date, s1.end_date, s2.end_date) As end_date
FROM
periods As S1 INNER JOIN periods As S2 ON
s1.person_id = s2.person_id And
s1.id < s2.id And
s2.start_date <= s1.end_date And s2.end_date >= s1.start_date
但结果存在问题:
person_id start_date end_date
1 01/09/2014 15/10/2014
1 25/09/2014 31/10/2014
1 15/11/2014 10/12/2014
请注意,第一行和第二行也是重叠时段。我可以管理这个用自己的结果执行相同的查询,但感觉很奇怪。
我需要什么:
我现在遇到的其他问题是我不知道如何:
不幸的是我只能使用MsAccess,因此我无法使用我发现谷歌搜索的一些技巧,因此我在这里问。
答案 0 :(得分:2)
您可以使用日历表解决此问题(下一个'n'年每天一行)。
create table calendar (
`id` COUNTER (1,1) PRIMARY KEY,
`calendar_date` Date
)
insert into calendar values ('2015-01-01')
insert into calendar values ('2015-01-02')
insert into calendar values ('2015-01-03')
insert into calendar values ('2015-01-04')
insert into calendar values ('2015-01-05') ... et cetera
然后:
select distinct calendar_date
from periods p join calendar c
where c.calendar_date between p.start_date and p.end_date and
not exists(select * from exclusions where c.calendar_date between e.start_date and e.end_date)