对于纯 SQL 解决方案,我们可以使用具有单个 Long Integer 列的 [Numbers] 表,该列至少包含值 0 到 1439,例如,
Integer
-------
0
1
2
3
...
1439
我们可以首先在 Access 中创建一个名为[X_with_minutes]的已保存查询,该查询检索 [X] 表中的数据并添加两列显示:(1) 仅显示日期,以及 (2) 午夜过后的分钟数...
SELECT X.*,
DateSerial(Year([Dates]), Month([Dates]), Day([Dates])) AS JustDate,
DateDiff("n", DateSerial(Year([Dates]), Month([Dates]), Day([Dates])), [Dates]) AS DayMinutes
FROM X;
...返回:
Dates Number1 Number2 JustDate DayMinutes
------------------- ------- ------- ---------- ----------
2000-01-01 00:00:00 0.3 4 2000-01-01 0
2000-01-01 00:02:00 0.5 5 2000-01-01 2
2000-01-01 00:05:00 0.8 4 2000-01-01 5
我们可以在 Access 中创建另一个名为[X_minutes_all]的已保存查询,以在 [X] 中生成每一天的所有分钟...
SELECT DISTINCT X_with_minutes.JustDate, Numbers.Integer AS DayMinutes
FROM X_with_minutes, Numbers
WHERE Numbers.Integer BETWEEN 0 AND 1439;
...返回:
JustDate DayMinutes
---------- ----------
2000-01-01 0
2000-01-01 1
2000-01-01 2
2000-01-01 3
...
2000-01-01 1439
现在我们可以创建一个返回缺失分钟数的查询并将该查询保存为[X_missing_minutes]
SELECT * FROM X_Minutes_all
WHERE NOT EXISTS
(
SELECT * FROM X_with_minutes
WHERE X_with_minutes.JustDate=X_minutes_all.JustDate
AND X_with_minutes.DayMinutes=X_minutes_all.DayMinutes
);
...返回:
JustDate DayMinutes
---------- ----------
2000-01-01 1
2000-01-01 3
2000-01-01 4
2000-01-01 6
2000-01-01 7
2000-01-01 8
...
2000-01-01 1439
要找到缺失分钟的“下一”分钟值,我们需要找到超过当前值的最小 (MIN) 值。我们可以如下创建该查询,并将其保存为[X_next_minutes] ...
SELECT X_missing_minutes.JustDate,
X_missing_minutes.DayMinutes,
(
SELECT MIN(DayMinutes)
FROM X_with_minutes
WHERE X_with_minutes.JustDate=X_missing_minutes.JustDate
AND X_with_minutes.DayMinutes>X_missing_minutes.DayMinutes
) AS NextMinute
FROM X_missing_minutes;
……归来……
JustDate DayMinutes NextMinute
---------- ---------- ----------
2000-01-01 1 2
2000-01-01 3 5
2000-01-01 4 5
2000-01-01 6 <NULL>
2000-01-01 7 <NULL>
2000-01-01 8 <NULL>
...
2000-01-01 1439 <NULL>
我们可以使用它来返回缺失分钟的 [X] 值......
SELECT DateAdd("n", X_next_minutes.DayMinutes, X_next_minutes.JustDate) AS Dates,
X_with_minutes.Number1, X_with_minutes.Number2
FROM X_next_minutes INNER JOIN X_with_minutes
ON X_next_minutes.JustDate=X_with_minutes.JustDate
AND X_next_minutes.NextMinute=X_with_minutes.DayMinutes;
……归来……
Dates Number1 Number2
------------------- ------- -------
2000-01-01 00:01:00 0.5 5
2000-01-01 00:03:00 0.8 4
2000-01-01 00:04:00 0.8 4
...我们可以使用原始 [X] 来添加 UNION ALL 以产生最终结果...
SELECT DateAdd("n", X_next_minutes.DayMinutes, X_next_minutes.JustDate) AS Dates,
X_with_minutes.Number1, X_with_minutes.Number2
FROM X_next_minutes INNER JOIN X_with_minutes
ON X_next_minutes.JustDate=X_with_minutes.JustDate
AND X_next_minutes.NextMinute=X_with_minutes.DayMinutes
UNION ALL
SELECT * FROM X
ORDER BY 1;
……归来……
Dates Number1 Number2
------------------- ------- -------
2000-01-01 00:00:00 0.3 4
2000-01-01 00:01:00 0.5 5
2000-01-01 00:02:00 0.5 5
2000-01-01 00:03:00 0.8 4
2000-01-01 00:04:00 0.8 4
2000-01-01 00:05:00 0.8 4