现在,我在MS Access中有一个表X
,其中包含以下字段:
X (Table)
- Dates (Date/Time)
- Number1 (Double)
- Number2 (Long Integer)
现在假设它有以下三个记录:
2000-01-01 00:00:00,0.3,4
2000-01-01 00:02:00,0.5,5
2000-01-01 00:05:00,0.8,4
我想编写一个可以通过OLE DB调用的视图或其他内容来返回以下内容:
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
我希望用下一个可用数据“填写”缺少的会议记录。有可能吗?
答案 0 :(得分:1)
对于纯SQL解决方案,我们可以使用[Numbers]表和一个Long Integer列,其中至少值为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