MS Access 2016 - 比较同一表中的日期
Posted
技术标签:
【中文标题】MS Access 2016 - 比较同一表中的日期【英文标题】:MS Access 2016 - Compare dates within same table 【发布时间】:2019-10-23 16:55:42 【问题描述】:我有两个表,用户和作业。用户 ID 和作业 ID 都是其表的自动编号和主键。
用户表:
UID | Uname | Uphone | etc...
1 | Billy | 911 |
2 | Alan | 119 |
3 | Maria | 191 |
工作表:
JID | UID | Jtitle | Date | etc...
1 | 1 | PenTest | 12Aug
2 | 1 | Consult | 15Nov
3 | 2 | VulScan | 05Sep
4 | 2 | PenTest | 15Sep
5 | 1 | PenTest | 30Dec
6 | 2 | PenTest | 13Oct
7 | 3 | VulScan | 10Nov
我需要做的是获取每个 UID 的作业,并测量最近和未来发生的两个日期之间的天数。例如,今天是 10 月 23 日,所以我需要知道 Billy(UID 1)在 8 月 12 日到 11 月 15 日之间的天数。此外,如果将来没有安排工作,那么理想情况下,我希望 Alan (UID 2) 的“未来”单元格为空。
在我的脑海中,它看起来类似于下表。
UID | Past | Future | Difference |
1 | 12Aug | 15Nov | 95
2 | 13Oct | |
3 | | 10Nov |
几乎所有用户过去都至少有一份工作,但不能保证将来有工作。可能会创建一个用户并且他们的工作日期尚未到来,就像 Maria (UID 3) 一样,但这将是一个有限的场景(用户从雇用到工作很可能不到 30 天)。
这几天我一直在想办法解决这个问题,但一无所获。也许我的代码不正确,但我无法计算出两个左连接,然后就出现了计算差异的问题。
【问题讨论】:
为什么 UID=1 是 11 月 15 日而不是 12 月 30 日? 用户 3 过去没有工作,但你说所有用户都有一个。 forpas:因为 15Nov 是最接近今天的日期。戈登·林诺夫:我在那里有点胡思乱想。 “几乎所有用户”就是我的意思。用户没有工作的情况很少见,但这种情况会发生。 【参考方案1】:这是一种解决方案(对于 sqlserver,msaccess sql 更靠后)——当日期等于今天时会发生什么?
-- for testing, declare table and insert some date into it
declare @Jobs table (JID int, UID int, Jtitle varchar(50), aDate datetime);
insert into @Jobs values (1, 1, 'PenTest', '12Aug2019');
insert into @Jobs values (2, 1, 'Consult', '15Nov2019');
insert into @Jobs values (3, 2, 'VulScan', '05Sep2019');
insert into @Jobs values (4, 2, 'PenTest', '15Sep2019');
insert into @Jobs values (5, 1, 'PenTest', '30Dec2019');
insert into @Jobs values (6, 2, 'PenTest', '13Oct2019');
insert into @Jobs values (7, 3, 'VulScan', '10Nov2019');
-- FULL Join the max(Past) and the min(Future) and convert the dates.
Select
COALESCE(a.UID, b.UID) as UID,
Left(Convert(varchar, Past, 106), 6) as Past,
LEFT(Convert(varchar, Future, 106), 6) as Future,
DATEDIFF(day, Past, Future) as Difference
FROM
(Select UID, Max(aDate) as Past from @Jobs where aDate <= GETDATE() Group By UID)a
FULL JOIN
(Select UID, Min(aDate) as Future from @Jobs where aDate > GETDATE() Group By UID) b
ON a.UID = B.UID
结果是--
UID Past Future Difference
1 12 Aug 15 Nov 95
2 13 Oct NULL NULL
3 NULL 10 Nov NULL
MSACCESS 的 SQL 使用左连接和右连接的 UNION 来获得相同的结果--
Select
NZ(a.UID, b.UID) as UID,
format( aPast, "ddMMM") as Past,
format( aFuture, "ddMMM") as Future,
DATEDIFF("d", aPast, aFuture) as Difference
FROM
(Select UID, Max(aDate) as aPast from Jobs where aDate <= DATE() Group By UID)a
LEFT JOIN
(Select UID, Min(aDate) as aFuture from Jobs where aDate > DATE() Group By UID) b
ON a.UID = B.UID
UNION
Select
NZ(a.UID, b.UID) as UID,
format( aPast, "ddMMM") as Past,
format( aFuture, "ddMMM") as Future,
DATEDIFF("d", aPast, aFuture) as Difference
FROM
(Select UID, Max(aDate) as aPast from Jobs where aDate <= DATE() Group By UID)a
RIGHT JOIN
(Select UID, Min(aDate) as aFuture from Jobs where aDate > DATE() Group By UID) b
ON a.UID = B.UID
要获取 Uname,只需将上面的内容包装为子选择并将其与用户连接。我必须将 UID-s 转换为整数 (Cint) 才能使其工作。
SELECT u.Uname, z.*
FROM (
Select
NZ(a.UID, b.UID) as UID,
format( aPast, "ddMMM") as Past,
format( aFuture, "ddMMM") as Future,
DATEDIFF("d", aPast, aFuture) as Difference
FROM
(Select UID, Max(aDate) as aPast from Jobs where aDate <= DATE() Group By UID)a
LEFT JOIN
(Select UID, Min(aDate) as aFuture from Jobs where aDate > DATE() Group By UID) b
ON a.UID = B.UID
UNION
Select
NZ(a.UID, b.UID) as UID,
format( aPast, "ddMMM") as Past,
format( aFuture, "ddMMM") as Future,
DATEDIFF("d", aPast, aFuture) as Difference
FROM
(Select UID, Max(aDate) as aPast from Jobs where aDate <= DATE() Group By UID)a
RIGHT JOIN
(Select UID, Min(aDate) as aFuture from Jobs where aDate > DATE() Group By UID) b
ON a.UID = B.UID
) as z
Left Join [User] as u
On cint(z.UID) = cint(u.UID)
我住在奥克兰和伯克利之间,我们有很多当地的啤酒吧。
【讨论】:
如果工作的日期与今天相同,我对它在过去或未来的表现感到满意。我可以把 = 放在任何一个上。我喜欢你的解决方案的外观,我会在早上测试它并标记它是否有效。谢谢。 @Ok55A1MGfL msaccess sql 解决方案已在答案中进一步添加,并且具有左右连接的 UNION。 如果可以的话,我会请你喝啤酒。您的解决方案完美运行。不过,我有点搞砸了我的问题,因为我要求的是 UID 而不是 Uname。我以为我可以拉 Uname,但我想不通。 我住在欧洲,可悲的是,这让我离加利福尼亚只有几公里。如果您能够提供更多帮助,我已在此处发布此问题的更新。 ***.com/questions/58747356/…以上是关于MS Access 2016 - 比较同一表中的日期的主要内容,如果未能解决你的问题,请参考以下文章
MS-Access 2016 中的“同一张表”LEFT JOIN ON“同一个公共字段”